Voiced by Amazon Polly |
Introduction
Amazon Web Services (AWS) Python library Boto3 offers a programmatic interface for interfacing with many AWS services, including Amazon CloudFormation.
Additionally, substantial capability for managing stack resources, accessing stack data, and monitoring stack events is offered by Boto3 CloudFormation, making it a potent tool for controlling infrastructure on AWS.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
Steps to Deploy AWS CloudFormation using Boto3
AWS Console, AWS CLI, and other methods can create an AWS CloudFormation stack. Using AWS CLI, CodePipeline, and other tools, we can also automate the building of the CloudFormation stack. In this blog post, we’ll explore how to automate the building of an AWS CloudFormation Stack using the AWS SDK for Python.
To automate the building of a AWS CloudFormation stack, follow the procedures listed below.
- Make that the Python version you wish to execute the Python script has the most recent updates installed. Macs already have Python installed by default.
2. Install the AWS Toolkit for Visual Studio Code
3. To create a CloudFormation stack automatically, you need two files.
Your Python script, MyCFNStackCreation.py
Your parameter file is named MyStackParameter.json.
4. Maintain these two files in the same directory where you will run the Python script.
For reference, see the Python script below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import boto3 from botocore.session import get_session from os.path import dirname boto3.setup_default_session() script_dir= dirname(__file__) print(script_dir) sees= boto3.Session(region_name='us-west-2') cfnclient= sees.client (‘cloudformation’) stack=’ ’ with open(f"{script_dir}/ MyStackParameter.json",'r') as fd: stack = fd.read() print(stack) params =[ { "ParameterKey": 'HashKeyElementName ', "ParameterValue":'Product_id' }, ] cfnclient.create_stack(StackName="myfirststack1", TemplateBody=stack,Parameters=params) print(cfnclient.describe_stacks()) |
6. For reference, see the parameter JSON file below. This is the template parameter JSON file, which can be altered to suit the needs of the AWS CloudFormation build. In this case, I’m using this parameter file to call the master stack. To create a Amazon DynamoDB table, the master stack contains nested stacks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
{ "AWSTemplateFormatVersion" : "2010-09-09", "Description" : "AWS CloudFormation Sample Template DynamoDB_Table.", "Parameters" : { "HashKeyElementName" : { "Description" : "HashType PrimaryKey Name", "Type" : "String", "AllowedPattern" : "[a-zA-Z0-9]*", "MinLength": "1", "MaxLength": "2048", "ConstraintDescription" : "must contain only alphanumberic characters" }, "HashKeyElementType" : { "Description" : "HashType PrimaryKey Type", "Type" : "String", "Default" : "S", "AllowedPattern" : "[S|N]", "MinLength": "1", "MaxLength": "1", "ConstraintDescription" : "must be either S or N" }, "ReadCapacityUnits" : { "Description" : "Provisioned read throughput", "Type" : "Number", "Default" : "5", "MinValue": "5", "MaxValue": "10000", "ConstraintDescription" : "must be between 5 and 10000" }, "WriteCapacityUnits" : { "Description" : "Provisioned write throughput", "Type" : "Number", "Default" : "10", "MinValue": "5", "MaxValue": "10000", "ConstraintDescription" : "must be between 5 and 10000" } }, "Resources" : { "myDynamoDBTable" : { "Type" : "AWS::DynamoDB::Table", "Properties" : { "AttributeDefinitions": [ { "AttributeName" : {"Ref" : "HashKeyElementName"}, "AttributeType" : {"Ref" : "HashKeyElementType"} } ], "KeySchema": [ { "AttributeName": {"Ref" : "HashKeyElementName"}, "KeyType": "HASH" } ], "ProvisionedThroughput" : { "ReadCapacityUnits" : {"Ref" : "ReadCapacityUnits"}, "WriteCapacityUnits" : {"Ref" : "WriteCapacityUnits"} } } } }, "Outputs" : { "TableName" : { "Value" : {"Ref" : "myDynamoDBTable"}, "Description" : "Table name of the newly created DynamoDB table" } } } |
7. Once the two necessary files (the parameter file and the Python script) have been saved in one directory, open the terminal and navigate to the two files’ directories.
8. Execute the Python Script
9. You can monitor the script’s progress on the console. You can use a Python script to deploy the CloudFormation stack using the above mentioned procedures.
Conclusion
Infrastructure management on Amazon can be substantially simplified by creating AWS CloudFormation stacks using Boto3. Boto3 facilitates deployment and lowers the possibility of human error by automating the development and configuration of resources.
Overall, Boto3’s deployment of AWS CloudFormation stacks has many advantages for Amazon users, including improved productivity, dependability, and scalability. Businesses may save time and money while constructing reliable and scalable infrastructure on AWS by utilizing the power of automation.
Making IT Networks Enterprise-ready – Cloud Management Services
- Accelerated cloud migration
- End-to-end view of the cloud environment
About CloudThat
CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 850k+ professionals in 600+ cloud certifications and completed 500+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, AWS GenAI Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner, Amazon CloudFront Service Delivery Partner, Amazon OpenSearch Service Delivery Partner, AWS DMS Service Delivery Partner, AWS Systems Manager Service Delivery Partner, Amazon RDS Service Delivery Partner, AWS CloudFormation Service Delivery Partner, AWS Config, Amazon EMR and many more.
FAQs
1. What is AWS CloudFormation?
ANS: – AWS CloudFormation is a tool that enables developers and companies to quickly assemble a group of linked AWS and outside resources, provision them, and manage them systematically and predictably.
2. What can developers do with AWS CloudFormation?
ANS: – In a straightforward, declarative manner that abstracts away the complexities of resource APIs, developers may deploy and upgrade compute, databases, and other resources. With the help of automatic rollbacks, automated state management, and resource management across accounts and regions, AWS CloudFormation enables the repeatable, predictable, and safe management of resource lifecycles. Recent improvements and options enable various resource creation methods, such as using the AWS CDK for programming in higher-level languages, configuration drift, and a new Registry that makes creating custom types that inherit many fundamental CloudFormation advantages simpler.
WRITTEN BY Rahul Kumar Jha
Comments