managing aws cloudformation templates managing aws cloudformation templates managing multiple...

Download Managing AWS CloudFormation Templates Managing AWS CloudFormation Templates Managing multiple CloudFormation

Post on 15-Jun-2020

1 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • 1 Managing AWS

    CloudFormation Templates Managing multiple CloudFormation template files needs more attention. It's high risk if we modify our AWS CloudFormation directly on production stage. In this chapter, we learn how to manage CloudFormation for development, testing and production stages.

    The following topics will be covered in this chapter:

    Introducing CloudFormation template lifecycle Introducing AWS CodePipeline Defining your testing and production stages Demo – deploying testing and production stages Managing testing and production stages Deleting CloudFormation stack from AWS CodePipeline

    Introducing CloudFormation template lifecycle Developing CloudFormation template has similar process in software development. If you have experiences in software development lifecycle (SDLC), there are many SDLC methodologies to perform this process. We can describe a general SDLC in the following diagram:

  • Managing AWS CloudFormation Templates Chapter 1

    [ 2 ]

    A general development lifecycle

    The preceding diagram shows a general SDLC that we can apply in CloudFormation development. There are five stages to build SDLC. The following is a list of stages:

    Requirement Design Developing Testing Evaluation

    There are many SDLC methodologies that we can apply to build AWS CloudFormation. A famous methodology is an agile methodology. We don't describe and focus on that topic. I recommended that you can read some books or articles related to software engineering.

    Introducing AWS CodePipeline If you have experiences SDLC with involving CI/CD, you will get the same experiences with AWS CodePipeline. Amazon AWS provides AWS CodePipeline services to manage

  • Managing AWS CloudFormation Templates Chapter 1

    [ 3 ]

    your software and application in development, testing and production stages. You can see AWS CodePipeline flows in the following diagram:

    AWS CodePipeline lifecycle

    The preceding diagram shows our AWS CodePipeline work. Starting from source codes, we can build and test. Depending on what kind of application type, we need to set up all requirements from building process included run time and libraries.

    AWS CodePipeline enables to build a machine for testing with AWS resources. You should be aware of resource cost that you use in the project. You can access AWS CodePipeline dashboard on http:/ ​/​console. ​aws. ​amazon. ​com/ ​codepipeline/ ​home. You can see it in the following screenshot:

    AWS CodePipeline official website

    In this chapter, we will explore AWS CodePipeline to develop AWS CloudFormation. We

  • Managing AWS CloudFormation Templates Chapter 1

    [ 4 ]

    define some stages on next section.

    Define your testing and production stages In the real projects, you probably define some stages before you launch your products. In a context of infrastructure, we should prepare for testing and production environments. If you implement infrastructure environment using AWS solution, you can implement AWS CloudFormation with applying CodePipeline.

    We can separate testing and production environment with CloudFormation template. You define some configurations and parameters related to testing stage or production stage. If you find some missing or fault configurations, you can remove and then perform provisioning your CloudFormation template easily.

    Next, we will see deploying, testing and production for system environment with AWS CloudFormation and CodePipeline.

    Demo – deploying testing and production stages In this section, we try to make practices by implementing testing and production stages using AWS CloudFormation and AWS CodePipeline. You can see in the following diagram for our project scenario. We develop CloudFormation template with utilizing AWS Lambda and AWS DynamoDB.

    Consider we have a system with AWS Lambda and AWS DynamoDB. We will deploy this system using AWS CloudFormation and AWS CodePipeline. We will implement production and test stages in our project:

  • Managing AWS CloudFormation Templates Chapter 1

    [ 5 ]

    System architecture for demo

    To implement our project, we will perform some steps as follows:

    Writing CloudFormation template Preparing a storage for CloudFormation

    Next, we will create a CloudFormation template for our project.

    Writing CloudFormation template The first step is to write a CloudFormation template. We create three files: lambda-func- instance.json, prod-stack-configuration.json, and test-stack- configuration.json.

    We create all AWS resources included common configuration in lambda-func- instance.json. In this demo, we create AWS Lambda function that accesses DynamoDB. We modify program codes from Chapter 5, Building Lambda Functions Using AWS CloudFormation. A complete program from lambda-func-instance.json can be seen as

  • Managing AWS CloudFormation Templates Chapter 1

    [ 6 ]

    follows:

    { "Description" : "AWS CloudFormation: Lambda and DynamoDB Table", "Parameters" : { "LambdaFunctionName":{ "Description": "AWS Lambda function name", "Type": "String" } }, "Resources" : { "TestLambdaFunction" : { "Type" : "AWS::Lambda::Function", "Properties" : { "FunctionName" : { "Ref": "LambdaFunctionName" }, "Handler" : "index.handler", "Role" : { "Fn::GetAtt" : ["TestLambdaExecutionRole", "Arn"] }, "Code" : { "ZipFile" : { "Fn::Join" : [ "\n", [ "var AWS = require('aws-sdk');", "var ddb = new AWS.DynamoDB();", "exports.handler = (event, context, callback) => {", " var params = {", " TableName: 'mydynamodb',", " Item: {", " 'id': {S:new Date().getTime().toString()},", " 'email': {S:event.email},", " 'name': {S:event.name},", " 'country' : {S:event.country},", " 'age' : {N:event.age},", " }", " };", " ddb.putItem(params, function(err, data) {", " if (err) {", " callback(err, 'Error');", " } else {", " callback(null, 'Insert data was succeed');", " }", " });", "}" ]]} }, "Timeout" : "10", "Runtime" : "nodejs6.10"

  • Managing AWS CloudFormation Templates Chapter 1

    [ 7 ]

    } }, "myDynamoDBTable" : { "Type" : "AWS::DynamoDB::Table", "Properties" : { "TableName": "mydynamodb", "AttributeDefinitions": [ {"AttributeName" : "id", "AttributeType" : "S"} ], "KeySchema": [ { "AttributeName": "id", "KeyType": "HASH" } ], "ProvisionedThroughput" : { "ReadCapacityUnits" : "5", "WriteCapacityUnits" : "5" } } }, "TestLambdaExecutionRole": { "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" ] }, "Action": [ "sts:AssumeRole" ] } ] }, "ManagedPolicyArns": ["arn:aws:iam::aws:policy/service- role/AWSLambdaBasicExecutionRole"], "Policies": [{ "PolicyName": "dynamodb", "PolicyDocument": { "Version": "2012-10-17", "Statement": [{ "Sid": "1", "Effect": "Allow", "Action": [ "dynamodb:PutItem"

  • Managing AWS CloudFormation Templates Chapter 1

    [ 8 ]

    ], "Resource": [ {"Fn::Join" : ["", ["arn:aws:dynamodb:", {"Ref": "AWS::Region"}, ":", {"Ref": "AWS::AccountId"}, ":table/mydynamodb"]]} ] }] } }], "Path": "/"

    } } } }

    Save these scripts into a file, called lambda-func-instance.json.

    Since we work with testing and production stages, we need configure each stage with different files. test-stack-configuration.json consists of testing parameters. For production, we set environment parameters on prod-stack-configuration.json file.

    For simple demo, we only change Lambda function name on testing and production stages. The following is a content of scripts on test-stack-configuration.json file:

    { "Parameters" : { "LambdaFunctionName" : "MyLambdaTest" } }

    We also def