Giter Club home page Giter Club logo

cognito-custom-email-sender-lambda's Introduction

Cognito Custom Email and SMS Sender Lambda trigger

Deploy with CloudFormation and Terraform.

Code for Send AWS Cognito emails with 3rd party ESPs

Install npm dependencies

cd lambda

docker run -it --rm -v $(pwd):/var/app node:12 bash

npm i

Cloudformation

docker run --rm -it -v $(pwd):/var/app --entrypoint bash amazon/aws-cli
aws configure
aws s3api create-bucket --bucket cognito-custom-email-sender-lambda-assets
aws cloudformation validate-template --template-body file://cloudformation/stack.yaml
{
    "Parameters": [
        {
            "ParameterKey": "ProjectName",
            "NoEcho": false,
            "Description": "Project name"
        },
        {
            "ParameterKey": "SendgridApiKey",
            "NoEcho": false,
            "Description": "Sendgrid API key"
        },
        {
            "ParameterKey": "CallingUserArn",
            "NoEcho": false,
            "Description": "Calling user ARN"
        }
    ],
    "Capabilities": [
        "CAPABILITY_IAM"
    ],
    "CapabilitiesReason": "The following resource(s) require capabilities: [AWS::IAM::Role]"
}
aws cloudformation package --template-file cloudformation/stack.yaml --s3-bucket cognito-custom-email-sender-lambda-assets --output-template-file cloudformation/stack-packaged.yaml

Successfully packaged artifacts and wrote output template to file cloudformation/stack-packaged.yaml.
Execute the following command to deploy the packaged template
aws cloudformation deploy --template-file /var/app/cloudformation/stack-packaged.yaml --stack-name <YOUR STACK NAME>
aws cloudformation deploy --template-file cloudformation/stack-packaged.yaml --stack-name cognito-custom-email-sender-cf-stack --parameter-overrides ProjectName=cognito-custom-email-sender CallingUserArn="$(aws sts get-caller-identity --query Arn --output text)" SendgridApiKey="<API_KEY>" --capabilities CAPABILITY_IAM

Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - cognito-custom-email-sender-cf-stack
aws cloudformation describe-stacks --stack-name cognito-custom-email-sender-cf-stack --query "Stacks[0].Outputs"
[
    {
        "OutputKey": "UserPoolClientId",
        "OutputValue": "3625vec7....",
        "Description": "User pool client ID"
    },
    {
        "OutputKey": "UserPoolId",
        "OutputValue": "us-east-1_CV7g...",
        "Description": "User pool ID"
    }
]
aws cognito-idp sign-up --client-id <CLIENT_ID> --username [email protected] --password <PASSOWORD> --user-attributes Name="name",Value="Max Ivanov"
{
    "UserConfirmed": false,
    "CodeDeliveryDetails": {
        "Destination": "h***@m***.io",
        "DeliveryMedium": "EMAIL",
        "AttributeName": "email"
    },
    "UserSub": "51c9045e-2f3e-4..."
}
aws cloudformation delete-stack --stack-name cognito-custom-email-sender-cf-stack

Terraform

docker run --rm -it -v $(pwd):/var/app --entrypoint bash amazon/aws-cli
aws configure
yum install -y yum-utils
Loaded plugins: ovl, priorities
amzn2-core                                                                                                                                                                                                                                                                                             | 3.7 kB  00:00:00
Resolving Dependencies
...
yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
Loaded plugins: ovl, priorities
adding repo from: https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
grabbing file https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo to /etc/yum.repos.d/hashicorp.repo
repo saved to /etc/yum.repos.d/hashicorp.repo
yum -y install terraform
Loaded plugins: ovl, priorities
hashicorp                                                                                                                                                                                                                                                                                              | 1.4 kB  00:00:00
hashicorp/2/x86_64/primary                                                                                                                                                                                                                                                                             |  39 kB  00:00:00
hashicorp                                                                                                                                                                                                                                                                                                             255/255
Resolving Dependencies
...
cd terraform

terraform init
# without update-user-pool null_resource
terraform apply -var="sendgrid_api_key=<API_KEY>" -var="project=tf-cognito-custom-email-sender" -var="update_user_pool_config_file=input.json"
aws cognito-idp update-user-pool --user-pool-id us-east-1_evzTb... --generate-cli-skeleton input
aws cognito-idp describe-user-pool --user-pool-id us-east-1_evzTb... --query UserPool > input.json
# with update-user-pool null_resource
terraform apply -var="sendgrid_api_key=<API_KEY>" -var="project=tf-cognito-custom-email-sender" -var="update_user_pool_config_file=input.json"
aws cognito-idp sign-up --client-id <CLIENT_ID> --username [email protected] --password <PASSOWORD> --user-attributes Name="name",Value="Max Ivanov"
{
    "UserConfirmed": false,
    "CodeDeliveryDetails": {
        "Destination": "h***@m***.io",
        "DeliveryMedium": "EMAIL",
        "AttributeName": "email"
    },
    "UserSub": "51c9045e-2f3e-4..."
}
terraform destroy -var="sendgrid_api_key=<API_KEY>" -var="project=tf-cognito-custom-email-sender" -var="update_user_pool_config_file=input.json"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.