aws-amplify / aws-amplify/amplify-cli

How to correctly add customEmailSender in the auth override.ts

Open
#12,833 7 comments 0 reactions 0 assignees View on GitHub
bug override p3
Dominant language
TypeScript
Stars
2.9k
Forks
825
Avg merge
11d 23h
Merged PRs (30d)
2

Description

### How did you install the Amplify CLI?

npm install -g @aws-amplify/cli

### If applicable, what version of Node.js are you using?

v14.20.1

### Amplify CLI Version

10.7.2

### What operating system are you using?

Mac

### Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

No

### Describe the bug

I am getting an error:
`Parameters: [customkmsKmsKeyArn] do not exist in the template` on deployment. When I'm checking the AWS Cloudformation events, I see that this error in Cloudformation is `AuthTriggerCustomLambdaStack`.

I was following this issue to set this up:
https://github.com/aws-amplify/amplify-cli/issues/11824

override.ts for auth category:
```
export function override(resources: AmplifyAuthCognitoStackTemplate) {
// START - Custom Email Sender
const parameter = 'customkmsKmsKeyArn'
resources.addCfnParameter(
{
type: 'String',
description: 'my kms key arn',
default: 'NONE',
},
parameter
)

resources.userPool.addPropertyOverride('LambdaConfig', {
...resources.userPool.lambdaConfig,
KMSKeyID: {
Ref: parameter,
}
})

const customEmailSenderLambdaConfig = {
...resources.userPool.lambdaConfig
}

customEmailSenderLambdaConfig["customEmailSender"] = {
lambdaVersion: 'V1_0',
lambdaArn: {
"Fn::Sub": "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:amplifyapp-CognitoCustomEmailSender-${env}"
}
}

resources.userPool.lambdaConfig = customEmailSenderLambdaConfig

// START - Lambda Triggers
const newLambdaConfig = {
...resources.userPool.lambdaConfig,
}

if(!newLambdaConfig["postConfirmation"]) {
newLambdaConfig["postConfirmation"] = {
"Fn::Sub": "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${env}-amplifyapp-PostConfirmation-965f9a74965f9a74",
}
}
if(!newLambdaConfig["preSignup"]) {
newLambdaConfig["preSignup"] = {
"Fn::Sub": "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${env}-amplifyapp-PreSignup-965f9a74965f9a74",
}
}

resources.userPool.lambdaConfig = newLambdaConfig
// END - Lambda Triggers

// START - Email Configuration
const newEmailConfiguration = {
...resources.userPool.emailConfiguration,
}

newEmailConfiguration["sourceArn"] = {
"Fn::Sub": "arn:aws:ses:${AWS::Region}:${AWS::AccountId}:identity/support@amplifyapp.com",
}

resources.userPool.emailConfiguration = newEmailConfiguration
// END - Email Configuration

// START - Custom Attributes
const resourcesUserPoolSchema = resources.userPool.schema as SchemaAttributeProperty[]

const customAttribute = {
"attributeDataType": 'String',
"developerOnlyAttribute": false,
"mutable": true,
"name": 'app_customer_id',
"required": false
}

resourcesUserPoolSchema.push(customAttribute)
// END - Custom Attributes

}
```

custom/kms/kms-cloudformation-template.json
```
{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
"env": {
"Type": "String"
}
},
"Resources": {
"CognitoCustomEmailSenderKmsKey": {
"Type": "AWS::KMS::Key",
"Properties": {
"Description": {
"Fn::Sub": [
"KMS key for amplifyappCognitoCustomEmailSender-${env}",
{
"env": {
"Ref": "env"
}
}
]
},
"KeyPolicy": {
"Version": "2012-10-17",
"Id": "key-default-1",
"Statement": [
{
"Sid": "Enable IAM User Permissions",
"Effect": "Allow",
"Principal": {
"AWS": {
"Fn::Sub": "arn:aws:iam::${AWS::AccountId}:root"
}
},
"Action": "kms:*",
"Resource": "*"
}
]
}
}
},
"CognitoCustomEmailSenderKmsKeyAlias": {
"Type": "AWS::KMS::Alias",
"Properties": {
"AliasName": {
"Fn::Sub": [
"alias/${env}/amplifyappCognitoCustomEmailSenderKmsKey",
{
"env": {
"Ref": "env"
}
}
]
},
"TargetKeyId": {
"Ref": "CognitoCustomEmailSenderKmsKey"
}
}
}
},
"Outputs": {
"KmsKeyArn": {
"Description": "The ARN of the KMS key",
"Value": {
"Fn::GetAtt": [
"CognitoCustomEmailSenderKmsKey",
"Arn"
]
}
}
},
"Description": "{\"createdOn\":\"Mac\",\"createdBy\":\"Amplify\",\"createdWith\":\"10.7.2\",\"stackType\":\"custom-customCloudformation\",\"metadata\":{}}"
}
```

backend-config.json
```
"auth": {
"amplifyapp965f9a74965f9a74": {
"customAuth": false,
"dependsOn": [
{
"attributes": [
"Arn",
"Name"
],
"category": "function",
"resourceName": "amplifyapp965f9a74965f9a74PostConfirmation",
"triggerProvider": "Cognito"
},
{
"attributes": [
"Arn",
"Name"
],
"category": "function",
"resourceName": "amplifyapp965f9a74965f9a74PreSignup",
"triggerProvider": "Cognito"
},
{
"attributes": [
"KmsKeyArn"
],
"category": "custom",
"resourceName": "kms"
}
],
...
```

Many thanks for help solving this.

### Expected behavior

AWS Amplify deployment works without issues and `customEmailSender` in lambda config is correctly set with KMS Key ID.

### Reproduction steps

1. add custom kms cloudformation with cloudformation for KMS and alias
2. add kms key arn attribute to the backend-config.json auth dependency
3. try to add kms key arn parameter to the override.ts and set customEmailSender for lambdaConfig

### Project Identifier

_No response_

### Log output

```
# Put your logs below this line

```

### Additional information

_No response_

### Before submitting, please confirm:

- [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
- [X] I have removed any sensitive information from my code snippets and submission.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.