DEV Community

Ayo
Ayo

Posted on

Beginner's AWS Guide: Containers and Infrastructure as Code (Part 8)

Objective:

This section explores containerisation and Infrastructure as Code (IaC) in AWS. We delve into Docker containers and how they solve deployment challenges, along with AWS ECS. We also examine the IaC service CloudFormation as a means for automating and managing cloud infrastructure through code rather than manual configuration!


Key IT Terminology: Containerisation πŸ“¦

Containerisation is like filling an isolated box with everything an application needs to run β€” instructions, tools, and supplies.

Once sealed, this box can be:

  • Shipped to any environment (local, test, production).
  • Duplicated to make identical containers.
  • Opened/ran anywhere with consistent behaviour β€” eliminating the β€œit only works on my machine” problem!

Image highlighting containerization architecture

Image sourced from: https://www.xenonstack.com/insights/containerization


Key IT Terminology: Docker 🐳

Docker is a popular containerisation platform as it helps us package an app and everything it needs to run on any system β€” not just the one it was built on.

When packaging an app with Docker, we typically include:

  • The application code
  • Dependencies (supporting libraries, frameworks, packages)
  • A runtime version (e.g. Node.js, Python, Java)
  • System files, like environment variables and config files (setup blueprints)
  • A minimal OS layer, e.g. Linux or Windows (not the full OS, just what is needed)

Docker Hierarchy: Containers are built in layers

  1. Dockerfile – A script with step-by-step instructions on what to put inside our box (our packing list).

  2. Docker Image – A snapshot built from the Dockerfile (the sealed box). We can store images in registries like DockerHub or Amazon ECR to reuse them later.

  3. Container – A running instance of that image (the opened box in action). We can run many containers from the same image!

Image showing the process of building a docker image, repositories to host our image, and running an instance of an image as a container


ECS: Elastic Container Service πŸ“¦

Amazon ECS is a container orchestration service which helps us to deploy, manage, and scale Docker containers across AWS infrastructure.

We can run containers on two types of infrastructure:

  1. EC2 launch type – We manage the server instances.
  2. Fargate launch type – AWS manages the servers (serverless approach!).

Image highlighting ECS core components: clusters, task definitions, tasks, services

Example: ECS Task Definition (EC2 launch type)

{
  "family": "simple-web-app",
  "containerDefinitions": [
    {
      "name": "web",
      "image": "nginx:latest",           // Docker image for the container
      "cpu": 256,                        // CPU units
      "memory": 512,                     // Memory in MB
      "portMappings": [                  // Port mapping for network configuration
        {
          "containerPort": 80,           // Port inside the container
          "hostPort": 80                 // Port on the ECS host
        }
      ],
      "environment": [                   // Environment variables
        {
          "name": "ENV",
          "value": "production"
        }
      ],
      "essential": true              // Marks this container as essential for the task
    }
  ],
  "requiresCompatibilities": ["EC2"],    // Compatible with EC2 launch type
  "cpu": "256",                          // Total CPU for the task
  "memory": "512",                       // Total memory for the task
  "taskRoleArn": "arn:aws:iam::123456789012:role/myTaskRole"
}
Enter fullscreen mode Exit fullscreen mode

Image highlighting CPU/memory allocation for container images in task definitions

πŸ’‘ EKS (Elastic Kubernetes Service) is another AWS orchestration tool β€” more advanced and Kubernetes-based.


CloudFormation: Infrastructure as Code (IaC) πŸ—οΈ

CloudFormation lets us write templates that define our desired AWS infrastructure using code rather than clicking through the AWS Console.

Templates are written in JSON or YAML (YAML is preferred), and can describe entire AWS environments, including EC2 instances, VPCs, databases, security groups, and their relationships.

CloudFormation Advantages:

Image highlighting the benefits of AWS CloudFormation including; IaC, cost management, & productivity

CloudFormation Hierarchy

CloudFormation Hierarchy explanation including; Templates, Stacks, StackSets

πŸ’‘ One template can lead to many different stacks depending on the input parameters and conditions we set in our JSON/YAML file!

Image noting how we upload and reference our templates using CloudFormation


CloudFormation: Template Components Overview

Table highlighting CloudFormation template sections and their purpose: AWSTemplateFormatVersion, Description, Resources, Parameters, Mappings, Conditions, Outputs


Image highlighting the execution of Mapping in CloudFormation

IaC Example: S3 Bucket


AWSTemplateFormatVersion: '2024-09-09'
Description: Simple S3 bucket for storing application data

Parameters:
  BucketName:
    Type: String
    Description: The name of the S3 bucket to create
    Default: my-simple-app-bucket

Resources:
  MyS3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Ref BucketName
      VersioningConfiguration:
        Status: Enabled
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true
        BlockPublicPolicy: true
        IgnorePublicAcls: true
        RestrictPublicBuckets: true

Outputs:
  S3BucketName:
    Description: Name of the created S3 bucket
    Value: !Ref MyS3Bucket

  S3BucketArn:
    Description: ARN of the created S3 bucket
    Value: !GetAtt MyS3Bucket.Arn

Enter fullscreen mode Exit fullscreen mode

🎯 TL;DR

  • Docker packages our app and everything it needs into portable containers.
  • ECS orchestrates and manages our Docker containers across AWS infrastructure.
  • CloudFormation templates define our infrastructure as code for consistency and repeatability, while stacks are running instances of our templates.

✨ This is part of a mini-series where I delve into everything cloud-related. Check out my other posts for further learning! ✨

Top comments (0)