Amazon CodeCatalyst is no longer open to new customers. Existing customers can continue to use the service as normal. For more information, see How to migrate from CodeCatalyst.
Tutorial: Deploy a serverless application
In this tutorial, you learn how to build, test, and deploy a serverless application as a CloudFormation stack using a workflow.
The application in this tutorial is a simple web application that outputs a 'Hello World' message. It consists of an AWS Lambda function and an Amazon API Gateway, and you build it using the AWS Serverless Application Model (AWS SAM), which is an extension of CloudFormation.
Topics
Prerequisites
Before you begin:
-
You need a CodeCatalyst space with a connected AWS account. For more information, see Creating a space.
-
In your space, you need an empty project called:
codecatalyst-cfn-projectUse the Start from scratch option to create this project.
For more information, see Creating an empty project in Amazon CodeCatalyst.
-
In your project, you need a CodeCatalyst environment called:
codecatalyst-cfn-environmentConfigure this environment as follows:
-
Choose any type, such as Non-production.
-
Connect your AWS account to it.
-
For the Default IAM role, choose any role. You'll specify a different role later.
For more information, see Deploying into AWS accounts and VPCs.
-
Step 1: Create a source repository
In this step, you create a source repository in CodeCatalyst. This repository is used to store the tutorial's source files, such as the Lambda function file.
For more information about source repositories, see Creating a source repository.
To create a source repository
-
In CodeCatalyst, in the navigation pane, choose Code, and then choose Source repositories.
-
Choose Add repository, and then choose Create repository.
-
In Repository name, enter:
codecatalyst-cfn-source-repository -
Choose Create.
You have now created a repository called
codecatalyst-cfn-source-repository.
Step 2: Create AWS roles
In this step, you create the following AWS IAM roles:
-
Deploy role – Grants the CodeCatalyst Deploy CloudFormation stack action permission to access your AWS account and CloudFormation service where you’ll deploy your serverless application. The Deploy CloudFormation stack action is part of your workflow.
-
Build role – Grants the CodeCatalyst build action permission to access your AWS account and write to Amazon S3 where your serverless application package will be stored. The build action is part of your workflow.
-
Stack role – Grants CloudFormation permission to read and modify the resources specified in the AWS SAM template that you will provide later. Also grants permission to CloudWatch.
For more information about IAM roles, see IAM roles in the AWS Identity and Access Management User Guide.
Note
To save time, you can create a single role, called the CodeCatalystWorkflowDevelopmentRole- role, instead of
the three roles listed previously. For more information, see Creating the CodeCatalystWorkflowDevelopmentRole-spaceName role for your account and space.
Understand that the spaceNameCodeCatalystWorkflowDevelopmentRole- role has very broad permissions that may pose a security
risk. We recommend that you only use this role in tutorials and scenarios where security is
less of a concern. This tutorial assumes you are creating the three roles listed
previously.spaceName
Note
A Lambda execution role is also required, but you don't need to create it now
because the sam-template.yml file creates it for you when you run the
workflow in step 5.
To create a deploy role
-
Create a policy for the role, as follows:
-
Sign in to AWS.
Open the IAM console at https://console.aws.amazon.com/iam/
. -
In the navigation pane, choose Policies.
-
Choose Create policy.
-
Choose the JSON tab.
-
Delete the existing code.
-
Paste the following code:
Note
The first time the role is used to run workflow actions, use the wildcard in the resource policy statement and then scope down the policy with the resource name after it is available.
"Resource": "*" -
Choose Next: Tags.
-
Choose Next: Review.
-
In Name, enter:
codecatalyst-deploy-policy -
Choose Create policy.
You have now created a permissions policy.
-
-
Create the deploy role, as follows:
-
In the navigation pane, choose Roles, and then choose Create role.
-
Choose Custom trust policy.
-
Delete the existing custom trust policy.
-
Add the following custom trust policy:
-
Choose Next.
-
In Permissions policies, search for
codecatalyst-deploy-policyand select its check box. -
Choose Next.
-
For Role name, enter:
codecatalyst-deploy-role -
For Role description, enter:
CodeCatalyst deploy role -
Choose Create role.
You have now created a deploy role with a trust policy and permissions policy.
-
-
Obtain the deploy role ARN, as follows:
-
In the navigation pane, choose Roles.
-
In the search box, enter the name of the role you just created (
codecatalyst-deploy-role). -
Choose the role from the list.
The role's Summary page appears.
-
At the top, copy the ARN value.
You have now created the deploy role with the appropriate permissions, and obtained its ARN.
-
To create a build role
-
Create a policy for the role, as follows:
-
Sign in to AWS.
Open the IAM console at https://console.aws.amazon.com/iam/
. -
In the navigation pane, choose Policies.
-
Choose Create policy.
-
Choose the JSON tab.
-
Delete the existing code.
-
Paste the following code:
Note
The first time the role is used to run workflow actions, use the wildcard in the resource policy statement and then scope down the policy with the resource name after it is available.
"Resource": "*" -
Choose Next: Tags.
-
Choose Next: Review.
-
In Name, enter:
codecatalyst-build-policy -
Choose Create policy.
You have now created a permissions policy.
-
-
Create the build role, as follows:
-
In the navigation pane, choose Roles, and then choose Create role.
-
Choose Custom trust policy.
-
Delete the existing custom trust policy.
-
Add the following custom trust policy:
-
Choose Next.
-
In Permissions policies, search for
codecatalyst-build-policyand select its check box. -
Choose Next.
-
For Role name, enter:
codecatalyst-build-role -
For Role description, enter:
CodeCatalyst build role -
Choose Create role.
You have now created a build role with a trust policy and permissions policy.
-
-
Obtain the build role ARN, as follows:
-
In the navigation pane, choose Roles.
-
In the search box, enter the name of the role you just created (
codecatalyst-build-role). -
Choose the role from the list.
The role's Summary page appears.
-
At the top, copy the ARN value.
You have now created the build role with the appropriate permissions, and obtained its ARN.
-
To create a stack role
-
Sign in to AWS using the account where you want to deploy your stack.
Open the IAM console at https://console.aws.amazon.com/iam/
. -
Create the stack role as follows:
-
In the navigation pane, choose Roles.
-
Choose Create role.
-
Choose AWS service.
-
In the Use case section, choose CloudFormation from the drop-down list.
-
Select the CloudFormation radio button.
-
At the bottom, choose Next.
-
Using the search box, find the following permissions policies, and then select their respective check boxes.
Note
If you search for a policy and it doesn't appear, make sure to choose Clear filters and try again.
-
CloudWatchFullAccess
-
AWSCloudFormationFullAccess
-
IAMFullAccess
-
AWSLambda_FullAccess
-
AmazonAPIGatewayAdministrator
-
AmazonS3FullAccess
-
AmazonEC2ContainerRegistryFullAccess
The first policy allows access to CloudWatch to enable stack rollbacks when an alarm occurs.
The remaining policies allow AWS SAM to access the services and resources in the stack that will be deployed in this tutorial. For more information, see Permissions in the AWS Serverless Application Model Developer Guide.
-
-
Choose Next.
-
For Role name, enter:
-