Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create Cloudformation templates programmatically

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to create a Cloudformation template programmatically, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

We will first install the tropospheric library:

$pip install troposphere

After the installation is complete, you can create a new file called helloworld-cf-template.py.

We will start our file by importing some definitions from the tropospheric module:

"" Generating CloudFormation template. "" from troposphere import (Base64, ec2, GetAtt, Join, Output, Parameter, Ref, Template,)

From a code perspective, the first thing we need to do is initialize a template variable. At the end of our script, the template will contain a complete description of our infrastructure, and we will be able to simply print its output to get our CloudFromation template:

T = Template ()

In this book, we will create and run multiple CloudFormation templates simultaneously. To help us identify the content in a given stack, we can provide a description. After creating the template, add the following description:

T.add_description ("Effective DevOps in AWS: HelloWorld web application")

When we started the EC2 instance using the Web command line interface, we chose which key pair to use to gain SSH access to the host. In order not to lose this ability, our template must first have a parameter that provides CloudFormation users with the ability to select the key pair to use when starting the EC2 instance.

To do this, we will create a Parameter object and initialize it by providing identifiers, descriptions, parameter types, descriptions, and constraint descriptions to help make the right decision

When we start the stack. To allow this parameter to exist in our final template, we will also use the add_paramter () function defined in the template class:

T.add_parameter (Parameter ("KeyPair", Description= "Name of an existing EC2 KeyPair to SSH", Type= "AWS::EC2::KeyPair::KeyName", ConstraintDescription= "must be the name of an existing EC2 KeyPair.",))

The next thing we want to look at is the security team. We will do exactly what we did with the KeyPair parameter.

ApplicationPort = 3000 t.add_resource (ec2.SecurityGroup ("SecurityGroup", GroupDescription= "Allow SSH and TCP/ {} access" .format (ApplicationPort), SecurityGroupIngress= [ec2.SecurityGroupRule (IpProtocol= "tcp", FromPort= "22", ToPort= "22", CidrIp= "0.0.0.0FromPort= 0",), ec2.SecurityGroupRule (IpProtocol= "tcp", FromPort=ApplicationPort, ToPort=ApplicationPort, CidrIp= "0.0.0.0x0",),],)

In the next section, we will no longer need to log in to our EC2 instance and manually install the helloworld.js file and its initialization script. To do this, we will take advantage of the UserData feature provided by EC2.

When you create an EC2 instance, you can provide a set of commands through the UserData optional parameters to run after the virtual machine starts.

User_data = Base64 (Join ("#! / bin/bash", "sudo yum install-- enablerepo=epel-y nodejs", "wget http://bit.ly/2vESNuc-O / home/ec2-user/helloworld.js", "wget http://bit.ly/2vVvT18-O / etc/init/helloworld.conf", "start helloworld"])

We will now focus on the main resource of our template, our EC2 instance. To create an instance, you need to provide the name used to identify the resource, image ID, instance type, security group, key pair for SSH access, and user data.

For simplicity, we will hard-code AMI ID (ami-a4c7edb2) and instance type (t2.micro).

The rest of the information needed to create our EC2 instance is security group information and key pair names, which we collected previously by defining parameters and resources. In CloudFormation, you can use the keyword Ref to refer to pre-existing sections of the template.

In the troposphere, this is done by calling the Ref () function. As before, we will add the result output to our template with the help of the add_resource function:

T.add_resource (ec2.Instance ("instance", ImageId= "ami-a4c7edb2", InstanceType= "t2.micro", SecurityGroups= [Ref ("SecurityGroup")], KeyName=Ref ("KeyPair"), UserData=user_data,)

In the last part of our script, we will focus on generating the output part of the template, which will be populated when CloudFormation creates the stack.

This selection allows you to print out useful information that is calculated during stack startup. In our example, there are two pieces of useful information to access the URL of our Web application and the public IP address of the instance so that we can access it through SSH as needed.

To retrieve such information, CloudFormation uses the function Fn::GetAtt. In the troposphere, this is converted to using the GetAttr () function:

T.add_output (Output ("InstancePublicIp", Description= "Public IP of our instance.", Value=GetAtt (instance, "PublicIp"),) t.add_output (Output ("WebUrl", Description= "Application endpoint", Value=Join ("", ["http://", GetAtt (instance," PublicDnsName "),": ", ApplicationPort])

At this point, we can have our script output the final result of the template we generated:

Print t.to_json () above is all the content of the article "how to create a Cloudformation template programmatically". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report