An AWS SAM template which creates an API Gateway API with Cognito authorizer and a Lambda function
- AWS CLI already configured with at least PowerUser permission
- AWS SAM CLI installed
Here is the how you can test the template on your side:
-
Download the sam-app.zip file and unzip it
-
Open the template.yaml and update the Lambda function ARN and UserPoolArn. Save the template.yaml file
Note: I hard-coded the function name and function ARN in the template. Under the AWS::Serverless::Function resource, I define the Lambda name as HelloWorldFunction
FunctionName: HelloWorldFunction
Under the AWS::Serverless::Api resource, The uri of the backend Lambda function is the following. The function name in the function ARN matches the FunctionName I defined for the Lambda function.
uri: "arn:aws:apigateway:us-east-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-2:<your-aws-account>:function:HelloWorldFunction:test/invocations"
-
Open the terminal and go to the unzipped folder
cd sam-app
- Build the application
sam build --use-container
- Package the application
sam package \
--output-template-file packaged.yaml \
--s3-bucket <your-s3-bucket-name>
- Deploy the application
sam deploy \
--template-file packaged.yaml \
--stack-name sam-app \
--capabilities CAPABILITY_IAM \
--region us-east-1
--region should be the same as the region of your S3 bucket
-
Open your CloudFormation console and you should be able to see a stack named sam-app
-
Once the Status of the stack becomes CREATE_COMPLETE, you can open the stack and see the API and Lambda function under the Outputs Section