aws-amplify / aws-amplify/amplify-cli

support Account and Region interpolation in functions' `custom-policies.json`

Open
#11,463 3 comments 11 reactions 0 assignees View on GitHub
feature-request functions p3
Dominant language
TypeScript
Stars
2.9k
Forks
825
Avg merge
11d 23h
Merged PRs (30d)
2

Description

### Is this feature request related to a new or existing Amplify category?

function

### Is this related to another service?

_No response_

### Describe the feature you'd like to request

Support Account and Region interpolation in functions' `custom-policies.json` file. Currently we need to hardcode account and region where wildcards do not suffice, and the documentation explicitly notes the use of wildcards is not best practice. ref https://github.com/aws-amplify/docs/issues/3972

> Note: Specifying resource or action as ‘*’ is not recommended as best practice. This gives the Amplify function resource Administrative privileges which should be avoided.

```json
[
{
"Action": ["ssm:GetParameters"],
"Resource": [
"arn:aws:ssm:*:*:parameter/*"
]
}
]
```

where we want to restrict by region and account

```json
[
{
"Action": ["ssm:GetParameters"],
"Resource": [
"arn:aws:ssm:${region}:${account}:parameter/*"
]
}
]
```

Today the CLI supports `${env}`, and this requests expands on the existing functionality. The suggestion also follows conventions used today in function CloudFormation templates:

```json
"Resource": {
"Fn::Sub": [
"arn:aws:logs:${region}:${account}:log-group:/aws/lambda/${lambda}:log-stream:*",
{
"region": {
"Ref": "AWS::Region"
},
"account": {
"Ref": "AWS::AccountId"
},
"lambda": {
"Ref": "LambdaFunction"
}
}
]
}
```

https://docs.amplify.aws/cli/function/#access-existing-aws-resource-from-lambda-function

Unfortunately we are unable to specify `AWS::Region` and `AWS::Acount` without receiving an error on push:

```
🛑 Invalid custom IAM policy for myfunction. Incorrect "Resource": arn:aws:ssm:${AWS::Region}:${AWS::Account}:parameter/*
```

### Describe the solution you'd like

see above

### Describe alternatives you've considered

hardcoding account and region details

### Additional context

_No response_

### Is this something that you'd be interested in working on?

- [ ] 👋 I may be able to implement this feature request

### Would this feature include a breaking change?

- [ ] ⚠️ This feature might incur a breaking change

Contributor guide

Open the contributing guide

Research direction

Start at the CLI's function custom-policies.json validation during amplify push and compare it with the existing ${env} interpolation behavior. Verify how custom IAM policies are transformed for function CloudFormation templates; done means ${region} and ${account} are accepted and produce region- and account-scoped resources without the current validation error.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cli, cloud, security
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.