Design a site like this with WordPress.com
Get started

[8] [ProjectX] – Automating Deployment using AWS CloudFormation

Source code: https://git.io/fj84C

In our last blog post, we saw how to automate deployment using AWS CLI. In this one, we will explore one more method of automatic deployment using AWS CloudFormation service.

CloudFormation is like a domain-specific language created for deployment. It is a declarative language wherein you create a file in YAML or JSON format to instruct AWS what resources you want created and how do you want their values configured.

It is more convenient than writing CLI or Scripts. Also, it is more clear, concise and intuitive once you get familiar with the syntax. Since it is native to AWS, it works with out any issues of compatibility and unknown surprises.

One of the biggest advantages of it is, it can do the complete deployment like a transaction, where in it either completes the full deployment or it does rollback if any of the resource fails to create. This is very important advantage, as with this, you are sure to leave your system in a consistent state.

Coming to our deployment automation, let’s assume that you are in Books business and have a master system (system of record) that contains your books catalog. This system has all the information regarding the books inventory as well as pricing and other systems in your enterprise frequently talk to this system via a bunch of Microservices you have developed.

Assume there is a popular Microservice called GetBookByIsbn. This service takes ISBN number and returns details of a book. Until now, this service was serving your internal systems but now you want this service to be published to internet so that other people will be able to do the same.

Hence, here what we would like to do is migrate your Microservice to AWS cloud. So here are the steps you need to undertake

  1. Migrate Books catalog database to AWS cloud
  2. Create Lambda function, which will encapsulate logic of your existing Microservices like GetBookByIsbn
  3. Put an API gateway over this Lambda function, so it is accessible to anyone on internet via REST API

We will ignore database part for now and proceed with Creation/Deployment of Lambda function and API Gateway using AWS CloudFormation. Now we need to build what is called a CloudFormation template.

Below is an example of template that creates a bucket called “myS3Bucket” in AWS S3

So, let’s get started with creation of a YAML CloudFormation template for our deployment

Create Lambda Function

This is what we are going to be building. Resources section is where you define what resources you want and how you want them created. Outputs section is for getting back information of resource created.

You can view the complete YAML file Here

STEP-1

Let’s start with creating a Role that Lambda function will use while executing. This role will contain permissions that define what Lambda function can and cannot do.

So, as you can see, we are creating a role and giving it a policy that contains permissions to do certain activity in AWS CloudWatch.  This will allow logging events to CloudWatch when Lambda function executes.

STEP-2:

Create getBooksByISBN Microservice as Lambda function

Right now, we keep this simple and just let it return hard-coded values of a book when it is executed. Also, you can see we are using previous defined LambdaExecutionRole in it.

This completes work related to Lambda.

Create API

Now we will continue with creation of API resources

STEP-1:

Create API

STEP-2:

Here we create getbook part of url, which calls getBookByISBN lambda function when you hit /getbook

STEP-3:

Here we create parameter resource to get isbn number in url (i.e. /getbook/isbn)

STEP-4:

Here we create the actual API GET method which will be called by users over internet

Note the Uri parameter. This is where we connect API GET method to our Lambda function (MyLambdaFunction)

STEP-5:

After creating an API, you need to Deploy it, to make it Live

STEP-6:

As last step in API, we need to set permission in API Gateway to allow it to call Lambda function. With this, we are done with API section of template.

Return Deployment Info

There is some information we want back from CloudFormation after deployment is completed successfully. This is defined in Outputs section

We are ready with our CloudFormation YAML template. Let’s proceed to deploy it

Execution of CloudFormation template

Login to AWS console and open CloudFormation service.

STEP-1

STEP-2:

Choose our YAML template file and click Next

STEP-3:

Give a name for the stack and click Next. Click Next in Options screen and tick check box “I acknowledge..” and click Create button

STEP-4:

AWS will begin deployment as per the template and start showing your progress

STEP-5:

When the process complete you will see status CREATE_COMPLETE as seen above

STEP-6:

Click on Outputs tab and you will see details related to Url for calling this API

STEP-7:

Let’s check whether Lambda function and API were created properly


Click on Stages on left menu and in dev stage in middle window, click GET link above /getbook/{isbn} and you will be able to get Invocation Url for this API

Everything looks good. So lets proceed to test it

STEP-8:

Open the Invoke url in browser or Postman and you should see it return data from our Lambda function

Congratulations! We did it.

Our getBookByIsbn Microservice has gone Global. Now it can be called by anyone on internet or from any website/mobile devices.

Clean up:

Cleaning up is also easy. Just go to your CloudFormation landing page, select our stack getbook-api-lambda-cf and click Actions drop-down and select Delete Stack option

After a few seconds, all the resources you created above will be deleted. Sweet. Isn’t it?

Conclusion:

As you have seen, deployment using CloudFormation is better than using AWS CLI. All it took is one YAML file and you are done.

Happy Clouding!

Advertisement

2 thoughts on “[8] [ProjectX] – Automating Deployment using AWS CloudFormation

  1. […] familiar with other ways provided by AWS for resource creation like AWS Console, AWS CLI, AWS SAM, AWS CloudFormation etc. Now we have one more option […]

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: