DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Building Scalable Data Lake Using AWS
  • Building a Scalable ML Pipeline and API in AWS
  • Breaking AWS Lambda: Chaos Engineering for Serverless Devs
  • AWS Step Functions Local: Mocking Services, HTTP Endpoints Limitations

Trending

  • How To Replicate Oracle Data to BigQuery With Google Cloud Datastream
  • Why We Still Struggle With Manual Test Execution in 2025
  • Scalable System Design: Core Concepts for Building Reliable Software
  • Beyond Microservices: The Emerging Post-Monolith Architecture for 2025
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. A Guide to Automating AWS Infrastructure Deployment

A Guide to Automating AWS Infrastructure Deployment

AWS CloudFormation automates infrastructure with code, supporting Elastic Beanstalk, serverless, EC2, and more for consistent deployments.

By 
Praveen Kumar Thopalle user avatar
Praveen Kumar Thopalle
·
Feb. 04, 25 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
4.2K Views

Join the DZone community and get the full member experience.

Join For Free

When it comes to managing infrastructure in the cloud, AWS provides several powerful tools that help automate the creation and management of resources. 

One of the most effective ways to handle deployments is through AWS CloudFormation. It allows you to define your infrastructure in a declarative way, making it easy to automate the provisioning of AWS services, including Elastic Beanstalk, serverless applications, EC2 instances, security groups, load balancers, and more.


AWS CloudFormation


In this guide, we'll explore how to use AWS CloudFormation to deploy infrastructure programmatically. We'll also cover how to manually deploy resources via the AWS Management Console and how to integrate services like Elastic Beanstalk, serverless functions, EC2, IAM, and other AWS resources into your automated workflow.

Using AWS CloudFormation for Infrastructure as Code

AWS CloudFormation allows you to define your infrastructure using code. CloudFormation provides a unified framework to automate and version your infrastructure by setting up Elastic Beanstalk, EC2 instances, VPCs, IAM roles, Lambda functions, or serverless applications.

CloudFormation templates are written in YAML or JSON format, and they define the resources you need to provision. With CloudFormation, you can automate everything from simple applications to complex, multi-service environments.

Key Features of CloudFormation

  • Declarative configuration. Describe the desired state of your infrastructure, and CloudFormation ensures that the current state matches it.
  • Resource management. Automatically provisions and manages AWS resources such as EC2 instances, RDS databases, VPCs, Lambda functions, IAM roles, and more.
  • Declarative stack updates. If you need to modify your infrastructure, simply update the CloudFormation template, and it will adjust your resources to the new desired state.

Steps to Use CloudFormation for Various AWS Deployments

Elastic Beanstalk Deployment With CloudFormation

1. Write a CloudFormation Template

Create a YAML or JSON CloudFormation template to define your Elastic Beanstalk application and environment. This template can include resources like EC2 instances, security groups, scaling policies, and even the Elastic Beanstalk application itself.

Example of CloudFormation Template (Elastic Beanstalk):

YAML
 
yaml
Resources:
  MyElasticBeanstalkApplication:
    Type: 'AWS::ElasticBeanstalk::Application'
    Properties:
      ApplicationName: "my-application"
      Description: "Elastic Beanstalk Application for my React and Spring Boot app"

  MyElasticBeanstalkEnvironment:
    Type: 'AWS::ElasticBeanstalk::Environment'
    Properties:
      EnvironmentName: "my-app-env"
      ApplicationName: !Ref MyElasticBeanstalkApplication
      SolutionStackName: "64bit Amazon Linux 2 v3.4.9 running Docker"
      OptionSettings:
        - Namespace: "aws:autoscaling:asg"
          OptionName: "MaxSize"
          Value: "3"
        - Namespace: "aws:autoscaling:asg"
          OptionName: "MinSize"
          Value: "2"
        - Namespace: "aws:ec2:vpc"
          OptionName: "VPCId"
          Value: "vpc-xxxxxxx"
        - Namespace: "aws:ec2:vpc"
          OptionName: "Subnets"
          Value: "subnet-xxxxxxx,subnet-yyyyyyy"


2. Deploy the CloudFormation Stack

Use the AWS CLI or AWS Management Console to deploy the CloudFormation stack. Once deployed, CloudFormation will automatically create all the resources defined in the template.

Deploy via AWS CLI:

YAML
 
bash
aws cloudformation create-stack --stack-name MyElasticBeanstalkStack --template-body file://my-template.yml


Serverless Deployment With AWS Lambda, API Gateway, and DynamoDB

CloudFormation is also great for deploying serverless applications. With services like AWS Lambda, API Gateway, DynamoDB, and S3, you can easily manage serverless workloads.

1. Create a Serverless CloudFormation Template

This template will include a Lambda function, an API Gateway for accessing the function, and a DynamoDB table.

Example of CloudFormation Template (Serverless):

YAML
 
yaml
Resources:
  MyLambdaFunction:
    Type: 'AWS::Lambda::Function'
    Properties:
      FunctionName: "MyServerlessFunction"
      Handler: "index.handler"
      Role: arn:aws:iam::123456789012:role/lambda-execution-role
      Code:
        S3Bucket: "my-serverless-code-bucket"
        S3Key: "function-code.zip"
      Runtime: nodejs14.x

  MyAPIGateway:
    Type: 'AWS::ApiGateway::RestApi'
    Properties:
      Name: "MyAPI"
      Description: "API Gateway for My Serverless Application"
    
  MyDynamoDBTable:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      TableName: "MyTable"
      AttributeDefinitions:
        - AttributeName: "id"
          AttributeType: "S"
      KeySchema:
        - AttributeName: "id"
          KeyType: "HASH"
      ProvisionedThroughput:
        ReadCapacityUnits: 5
        WriteCapacityUnits: 5


2. Deploy the Serverless Stack

Deploy your serverless application using the AWS CLI or AWS Management Console.

YAML
 
bash
aws cloudformation create-stack --stack-name MyServerlessStack --template-body file://serverless-template.yml


VPC and EC2 Deployment

CloudFormation can automate the creation of a Virtual Private Cloud (VPC), subnets, security groups, and EC2 instances for more traditional workloads.

1. CloudFormation Template for VPC and EC2

This template defines a simple EC2 instance within a VPC, with a security group allowing HTTP traffic.

Example of CloudFormation Template (VPC and EC2):

YAML
 
Resources:
  MyVPC:
    Type: 'AWS::EC2::VPC'
    Properties:
      CidrBlock: "10.0.0.0/16"
      EnableDnsSupport: "true"
      EnableDnsHostnames: "true"

  MySecurityGroup:
    Type: 'AWS::EC2::SecurityGroup'
    Properties:
      GroupDescription: "Allow HTTP and SSH traffic"
      SecurityGroupIngress:
        - IpProtocol: "tcp"
          FromPort: "80"
          ToPort: "80"
          CidrIp: "0.0.0.0/0"
        - IpProtocol: "tcp"
          FromPort: "22"
          ToPort: "22"
          CidrIp: "0.0.0.0/0"

  MyEC2Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      InstanceType: "t2.micro"
      ImageId: "ami-xxxxxxxx"
      SecurityGroupIds: 
        - !Ref MySecurityGroup
      SubnetId: !Ref MyVPC


2. Deploy the Stack

YAML
 
aws cloudformation create-stack --stack-name MyEC2Stack --template-body file://vpc-ec2-template.yml


Advanced Features of CloudFormation

AWS CloudFormation offers more than just simple resource provisioning. Here are some of the advanced features that make CloudFormation a powerful tool for infrastructure automation:

  • Stack Sets. Create and manage stacks across multiple AWS accounts and regions, allowing for consistent deployment of infrastructure across your organization.
  • Change Sets. Before applying changes to your CloudFormation stack, preview the changes with a change set to ensure the desired outcome.
  • Outputs. Output values from CloudFormation that you can use for other stacks or applications. For example, output the URL of an API Gateway or the IP address of an EC2 instance.
  • Parameters. Pass in parameters to customize your stack without modifying the template itself, making it reusable in different environments.
  • Mappings. Create key-value pairs for mapping configuration values, like AWS region-specific values, instance types, or other environment-specific parameters.

Using CloudFormation With AWS Services Beyond Elastic Beanstalk

CloudFormation isn't just limited to Elastic Beanstalk deployments — it's a flexible tool that can be used with a variety of AWS services, including:

  • AWS Lambda. Automate the deployment of serverless functions along with triggers like API Gateway, S3, or DynamoDB events.
  • Amazon S3. Use CloudFormation to create S3 buckets and manage their configuration.
  • AWS IAM. Automate IAM role and policy creation to control access to your resources.
  • Amazon RDS. Define RDS databases (MySQL, PostgreSQL, etc.) with all associated configurations like VPC settings, subnets, and security groups.
  • Amazon SQS, SNS. Manage queues and topics for your application architecture using CloudFormation.
  • Amazon ECS and EKS. Automate the creation and deployment of containerized applications with services like ECS and EKS.

Manually Deploying Infrastructure from the AWS Management Console

While CloudFormation automates the process, sometimes manual intervention is necessary. The AWS Management Console allows you to deploy resources manually.

1. Elastic Beanstalk Application

  • Go to the Elastic Beanstalk Console.
  • Click Create Application, follow the steps to define the application name and platform (e.g., Docker, Node.js), and then manually configure the environment, scaling, and security options.

2. Serverless Applications (Lambda + API Gateway)

  • Go to Lambda Console to create and deploy functions.
  • Use API Gateway Console to create APIs for your Lambda functions.

3. EC2 Instances

  • Manually launch EC2 instances from the EC2 Console and configure them with your chosen instance type, security groups, and key pairs.

Conclusion

AWS CloudFormation provides a consistent and repeatable way to manage infrastructure for Elastic Beanstalk applications, serverless architectures, and EC2-based applications. With its advanced features like Stack Sets, Change Sets, and Parameters, CloudFormation can scale to meet the needs of complex environments.

For anyone managing large or dynamic AWS environments, CloudFormation is an essential tool for ensuring consistency, security, and automation across all your AWS deployments.

AWS AWS Elastic Beanstalk AWS Lambda Infrastructure

Opinions expressed by DZone contributors are their own.

Related

  • Building Scalable Data Lake Using AWS
  • Building a Scalable ML Pipeline and API in AWS
  • Breaking AWS Lambda: Chaos Engineering for Serverless Devs
  • AWS Step Functions Local: Mocking Services, HTTP Endpoints Limitations

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: