Compile outputs fun

AWS CloudFormation: 05 - Create a web server Docker instance

Published 2 years agoAWS, CloudFormation

This is continued from AWS CloudFormation: 04 - Build and deploy pipeline .

Background

I want a web server Docker instance.

What we need?

  1. A VPC with 2 subnets because we need to have a load balancer also.
  2. A load balancer so we can have a fixed endpoint with flexible Docker instances.
  3. A container cluster to host the Docker web server.
  4. A container task definition to tell how to run the Docker web server.

How to do it?

This is what we are going to do:

Open 02-autoscaling-template.yaml from previous post and save as 05-container-webserver-template.yaml .

Unwanted Resources

We don't need all the resources from the previous YAML file, so we need to do some cleanup first. Delete these resources

  • WebServerLaunchTemplate
  • WebServerAutoScalingGroup

We don't need the SSH access also, so delete the port 22 from the WebServerSecurityGroup.

Load Balancer

The previous load balancer is setup to work with EC2 instances. We need to change it to work with Docker instances. Change this resource:

1   WebServerLoadBalancerTargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      Port: 80
      Protocol: HTTP
      VpcId: !Ref VPC
      TargetGroupAttributes:
        - Key: deregistration_delay.timeout_seconds
          Value: "30"
      TargetType: ip
Change the TargetType to ip so it works with Docker instances.

Container

We need to setup to container. We will use AWS Fargate service. It will handle all the Docker services for us. We just need to setup the container task definition. Add these resources:

1   WebServerContainerCluster:
    Type: AWS::ECS::Cluster
The container cluster.
2   WebServerContainerTaskDefinition:
    Type: AWS::ECS::TaskDefinition
    Properties:
      ContainerDefinitions:
        - Name: WebServer
          Image: nginx
          PortMappings:
            - ContainerPort: 80
      RequiresCompatibilities:
        - FARGATE
      NetworkMode: awsvpc
      Cpu: 256
      Memory: 512
The container task definition.
3   WebServerContainerService:
    Type: AWS::ECS::Service
    Properties:
      Cluster: !Ref WebServerContainerCluster
      DesiredCount: 1
      LaunchType: FARGATE
      TaskDefinition: !Ref WebServerContainerTaskDefinition
      NetworkConfiguration:
        AwsvpcConfiguration:
          AssignPublicIp: ENABLED
          SecurityGroups:
            - !Ref WebServerSecurityGroup
          Subnets:
            - !Ref PublicSubnet
      LoadBalancers:
        - ContainerName: WebServer
          ContainerPort: 80
          TargetGroupArn: !Ref WebServerLoadBalancerTargetGroup
    DependsOn: WebServerLoadBalancerHttpListener
Launch the container into the cluster.

Parameters

We have removed the SSH access to the Docker instance. We don't need any parameters here, so just remove all.

The URL to the load balancer is available at the Outputs section. Update the stack then you should be able to see the NGINX test page with the load balancer URL.

Next we will do AWS CloudFormation: 06 - Collect logs from Docker instance .