Any application that will undergo maintenance will want to have more than one environment. Keeping these environments updated where minimal human interaction is needed is a challenge. Thankfully there are many solutions out there and tools to accomplish this. We will show you one way that we found to be the easiest, less use of the Serverless framework product for account management, rely more on AWS services and remain secure.
Technologies
To accomplish deploying the same application configuration and code to different environments that exist in their own AWS account we’ll be using the following technologies:
- CodeBuild
- Serverless
- IAM User
- CodeCommit
- Secrets Manager
Serverless provides a template approach to describe our infrastructure that we want to create in our AWS account. This is similar to Cloudformation as Serverless template generates a Cloudformation template on deploy. Using Serverless provides benefits such as easier template configuration for common components such as API Gateway events that trigger lambda functions, along with framework bonuses like powerful monitoring.
CodeCommit will contain our code where CodeBuild will package and deploy our code to the proper AWS account using access keys stored in Secrets Manager.
IAM will provide permissions to only those that are in specific groups to access certain resources and keys to secure our environments and deployments.
Use Case
We want to have separate AWS accounts for each environment that our application will live in. Our Dev account will contain our code repository and the CodeBuilds for each environment. We want to be able to deploy to Dev, Test and Prod accounts through these CodeBuilds.
Environments
Development
- Developers push their own application stacks here to test their ongoing development branches
- Hosts the development shared application stack for integration testing of development branches merged to master (or feature branch) changes.
Test
- Hosts the application stack and test environment for integration testing the master (feature branch) changes.
Production
- Hosts the application stack and production environment for public customer and client interaction.
Overview
As you can see in diagram below, we have our Dev account host the code repository in CodeCommit and have CodeBuild instances to deploy to either the Dev account, Test account or Prod account. You may ask how all this is setup. Well let’s dive deeper into the configuration and what makes this tick.
Solution
The solution to deploy the application stack to each environment will be similar. We are also ensuring enough security and separation of concern is in place as each environment has their own allowed persons that are allowed access.
IAM Users and Secrets
We create an IAM User in each account that only has programmatic access. We will use these Access Key ID and Secret Access Key pairs to grant our CodeBuild access to deploy the application stack. To keep these keys secret, we will store them within Secrets Manager in the Dev account. The user’s of the Dev account will only have permission to the secrets that they should based on their role which we handle through a policy on User Groups.
Developers may not have access to any of these secrets. Admins may be the only persons allowed to view and edit these values.
We can write the policy around the environment prefix to only allow those that should have access to each account the proper prefix.
CodeBuilds
CodeBuild is our runtime to perform the build of the code and deploy application resources to the proper account within a Cloudformation stack. To do this for each account, we need to provide a CodeBuild instance a build script along with proper permissions to access the target account.
Configuration
Within the configuration of the CodeBuild instance, we set environment variables that allow the CodeBuild to pull the secrets that contain the IAM User credentials for the target account. This CodeBuild instance can read those secrets because the role associated to this CodeBuild has permission to access this specific secret.
buildspec
The CodeBuild instance will run the buildspec.yml that exists in the code repositories root. If you want a different buildspec.yml per CodeBuild instance, you may override this behaviour. The following buildspec.yml is an example for how to build a Serverless project using the credentials provided in the CodeBuild environment variables.
version: 0.2phases:install:
#If you use the Ubuntu standard image 2.0 or later, you must specify runtime-versions.
#If you specify runtime-versions and use an image other than Ubuntu standard image 2.0, the build fails.
runtime-versions:
nodejs: 10
commands:
- echo Entered the install phase
- npm install -g serverless
- cd ${SERVICE_PATH}
- npm install
pre_build:
commands:
- echo Entered the pre-build phase
- npm test
build:
commands:
- echo Entered the build phase
- echo Setting Serverless Framework Pro credentials to authenticate during deployment
- export SERVERLESS_ACCESS_KEY=${SERVERLESS_ACCESS_KEY}
- echo Setting default profile credentials
- serverless config credentials --provider aws --key ${ACCESS_KEY} --secret ${SECRET_KEY}
post_build:
commands:
- bash -c "if [ /"$CODEBUILD_BUILD_SUCCEEDING/" == /"0/" ]; then exit 1; fi"
- echo Building and deploying to ${BUILD_ENV}
- serverless deploy --stage=${BUILD_ENV} --aws-profile default -v
- echo Build completed on `date`
cache:
paths:
- ${SERVICE_PATH}/node_modules
Let’s break this build script down. There are four phases to the build that we are leveraging.
Install
We are executing three commands to install the dependencies for our application. Since our application exists in a monorepo, we need to navigate to that folder in our repository.
# install serverless command globally in container npm install -g serverless
# navigate to project in repo we want to build cd ${SERVICE_PATH}
# install project dependencies npm install
pre_build
We are running tests in this phase. If tests fail, the phase will end the build process with an error.
npm test
build
The build phase is where we are setting credentials to authenticate to Serverless Framework Pro as an environment variable in our current shell. Once that is done, we also configure the AWS credentials in our container using the supplied Access Key and Secret Key for an IAM user that has permissions to deploy to the target environment.
export SERVERLESS_ACCESS_KEY=${SERVERLESS_ACCESS_KEY}
serverless config credentials --provider aws --key ${ACCESS_KEY} --secret ${SECRET_KEY}
post_build
Now that everything is setup, we can now deploy the application to the target environment (AWS account). We first check that the build phase succeeded and then execute the Serverless deploy command to initiate deployment.
bash -c "if [ /"$CODEBUILD_BUILD_SUCCEEDING/" == /"0/" ]; then exit 1; fi"
serverless deploy --stage=${BUILD_ENV} --aws-profile default -v
We had set credentials in the build phase under the ‘default’ profile. So during our build, we are deploying using the ‘default’ profile so the right credentials are used to access the target AWS account.
Cache
So that future builds using this CodeBuild configuration is quicker, we cache the node_modules folder of all dependencies. This will speed up the ‘install’ phase as we do not need to download all the dependencies again but just copy from cache.
cache:
paths:
- ${SERVICE_PATH}/node_modules
Conclusion
With a push of a button on a CodeBuild, we can deploy effortlessly to a target AWS account. The deployment is secure as credentials are only viewable by those that should see them, even though others may be allowed to perform the deployment.



