Software DevelopmentLearn Efficient Deployment Of Apps on Amazon AWS

Learn Efficient Deployment Of Apps on Amazon AWS

When it comes to AWS, you can go for deployment in multiple ways. However, not every one of them knows how to efficiently do it. In today’s article, we will focus on how to do efficient deployment using Go cloud formation!

Almost every company use Cloud. And, why not? It manages their multiple systems and they can even scale the functions as per their need. It can range from web applications to simple CMS software the possibilities are endless.

Modern applications are also prone to new updates, now and then, leading to issues. That’s why it is essential for organizations to understand the importance of efficient deployment.

Generally, it is not recommended to deploy manually as it contains a lot of variables to manage and execute. Tools are used to simplify the whole process.

Let’s talk about AWS CloudFormation.

What is AWS CloudFormation

AWS CloudFormation lets you manage the AWS resources. It is a service and helps you to manage your resources so that you can spend more time on other important things. CloudFormation uses JSON.

So, how AWS Cloud Formation makes it easy for efficient deployment?

The answer is its ability to treat multiple services, modules or resources as a single unit. Yes, you read it right. It can basically treat different server resources as one, and let you manage them as a single stack unit. For example, you can treat the RDS data service, EC2 server platform as a single unit, making it easy for you to handle your resources. However, you need to be careful, as deleting a single stack can also delete its components without any warning.

Efficient Deployment on AWS

By managing to cluster all the services into one, you can deploy easily. Let’s go through the steps one by one.

Our example setup will require the use of multiple libraries and technology including Python, and Boto. You may need the Access key setup and AWS secret key. If you are not sure where to get them, you need to check the documentation. In short, we will be doing a serverless deployment using Lambda and API Gateway. By doing this, we will be able to deploy GET API that works with Python Lambda function. When used, the API will be able to return the user information when queried with the user id.

All our code will be written in JSON.

Step 1: – Use Ansible to create a playbook. We can name it “cf_serverless_deploy.yml.” Ansible will be useful to help you deploy with Cloud formation. It will act as a controller that will trigger the deployment process.

---
- name: deploying application.
 hosts: localhost
 tasks:
   - name: deploy the CF lambda
     cloudformation:
       stack_name: "ServerlessDeploy"
       state: present
       region: us-east-1
       template: cloudformation.json
     register: output
   - debug: msg="{{ output }}"

The name field should be changed according to what you are planning to deploy. The outer name field and the inner name field doesn’t need to be same.

Step 2: – Now that our controller is created. We now need to create a cloudformation template that will store the required information for the functions that needs to be called out. Let’s go through the code below.

"LambdaGET": {
     "Type": "AWS::Lambda::Function",
     "Properties": {
       "FunctionName": "lambdaget",
       "Code": {
           "S3Bucket": <bucket-name>,
           "S3Key": <bucket-key>
       },
       "Role": <role-name>,
       "Timeout": 30,
       "Handler": "get.main",
       "Runtime": "python2.7",
       "MemorySize": 128,
       "VpcConfig": {
         "SecurityGroupIds": [<sg-name>],
          "SubnetIds": [<subnet-name>]
       }
     }
   }

The key aspects of the above code are LambdaGET, Function name and Handler. The LambdaGET is the block identifier, whereas the Functionname is the name of the Lambda Function.

Also, the Handler is used acts as the entry point in the Python code.

Step 3: – Awesome, now that we have created the Ansible playbook and Cloudformation template, it is now time to create an API Gateway resource. This resource will list the type and properties.

"RestApi": {
   "Type": "AWS::ApiGateway::RestApi",
   "Properties": {"Name": "ServerLessGet"}
 }

Step 4: – It is now time to create an API resource file. It can easily be done using the following code.

V1Resource": {
   "Type": "AWS::ApiGateway::Resource",
   "Properties": {
     "RestApiId": {"Ref": "RestApi"},
     "ParentId": {"Fn::GetAtt": ["RestApi", "RootResourceId"]},
     "PathPart": "v1"
   }
 }

The two things that we really need to understand is the RestApiID and PathPart. The RestApiID lets you simply link to the previous block’s REST API. PathPart, on the other hand, is used to create the /v1 path.

Let’s create the UserResource now.

"UserResource": {
   "Type": "AWS::ApiGateway::Resource",
   "Properties": {
     "RestApiId": {"Ref": "RestApi"},
     "ParentId": {"Ref": "V1Resource"},
     "PathPart": "users"
   }
 }

It is similar to the code we have written above.

Step 5: – With everything created, our last step is simply to create the GetMethod that have all the required functionality.

"GetMethod": {
   "Type": "AWS::ApiGateway::Method",
   "Properties": {
     "RestApiId": {"Ref": "RestApi"},
     "ResourceId": {"Ref": "UserResource"},
     "HttpMethod": "GET",
     "AuthorizationType": "NONE",
     "RequestParameters": {
         "method.request.querystring.name": false,
         "method.request.querystring.id": false
       },
     "Integration": {
       "Type": "AWS",
       "IntegrationHttpMethod": "POST",
       "Uri": {"Fn::Join" : ["", ["arn:aws:apigateway:", {"Ref": "AWS::Region"}, ":lambda:path/2015-03-31/functions/", {"Fn::GetAtt": ["LambdaGET", "Arn"]}, "/invocations"]]},
       "RequestTemplates": {"application/json": "{\"id\": \"$input.params('id')\",\"name\": \"$input.params('name')\"}"},
       "IntegrationResponses": [
         {
           "SelectionPattern": "-",
           "StatusCode": 200
         },
         {
           "SelectionPattern": ".*InternalServerError.*",
           "StatusCode": 500
         }]
     },
     "MethodResponses": [
       {"StatusCode": 200},
       {"StatusCode": 500}
     ]
   }
 }

The whole code is self-explanatory. The Integration field is used to help you get API Gateway Lambda Integration.

Other tools that you should try out
There are many other tools that you can try out. These tools will help you automate the process and allow you to use your time in managing other aspects of your application.

Chef: Chef helps you automate and configure deployment in a cloud platform. It works with AWS and other cloud services as well.
Ansible: We used Ansible for this tutorial. You can read more about it and understand how it works in detail. It can be used as cloud application deployment, configuration management, and cloud provisioning.
AWS CodeDeploy: It is an in-built service that automates deployment on EC2 and AWS Lambda.

So, which method or tool you are going to use? Comment below and let us know.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -