Continuous Deployment of an AWS API Gateway using Bitbucket Pipelines

Kingyinma
3 min readApr 21, 2021

In this tutorial, I’ll build a simple NodeJS API which will be deployed in AWS Lambda and the endpoints will be exposed using AWS API gateway. The deployment will use Bitbucket pipelines which allow you to preform CICD when code change in a specific branch, also the configuration will be defined in AWS SAM template.

Cloudformation (SAM) allows you to manage your AWS infrastructure using just a yaml file. Using these two tools you can create a solid CD environment from scratch.

Suppose you have an AWS account and now create an IAM user with following policies

IAMFullAccess
AmazonS3FullAccess
AmazonAPIGatewayAdministrator
AWSCloudFormationFullAccess
AWSLambda_FullAccess

Save key pairs for later use

After created IAM, you need to create an NON-public S3 bucket in AWS console, all are default setting is fine since it will not make bucket as public by default.

Next, create a repository, I named lambda-cicd-demo as example

clone the repository to your local, I use mac and terminal as example

git clone https://{your_user_name}@bitbucket.org/{workspace}/lambda-cicd-demo.git

Now, create an index.js of lambda function

Create the SAM template

The bitbucket-pipelines yaml file below, inside, it will deploy when min branch has some changes and using Test environment variables

Next, go to bitbucket console, to define deployment, environment variables

Now you can push your code to bitbucket and

go back to AWS console, Search “API Gateway”

Click Name in the list, which is same as STACK_NAME you defined in bitbucket-pipelines yaml before.

You can see the information of the API and also a “Test” button to test the api

Click Test button

then it shows the result below

If you want to test the API which can call outside, the API url format is

https://{ID}.execute-api.{region}.amazonaws.com/Prod/{endpoint}

You can use postman as example

The same source is in here, Hope this helps! thanks

--

--