Lab8 – Laboratory Content



Deploying an infrastructure and application using CloudFormation

LAB Overview

In this lab you will create the the application running on ECS Fargate cluster together with Application Loadbalancer. Application will be deploying in Docker container from docker hub repository.

Task 1: Creating Cloud formation stack

In this task you will create CloudFormation stack with ECS Fargate cluster and Application Loadbalancer. You will use CloudFormation template stored in S3 bucket.

The template containe Parameters section.. Thanks to this you will be able to provide some information during stack creation.

Look at the floowing example:

Parameters:
  VPC:
    Type: AWS::EC2::VPC::Id
  SubnetA:
    Type: AWS::EC2::Subnet::Id
  SubnetB:
    Type: AWS::EC2::Subnet::Id
  Image:
    Type: String
    Default: kaminskypavel/mario
  ServiceName:
    Type: String
    # update with the name of the service
    Default: awsninjax-sn
  ContainerPort:
    Type: Number
    Default: 8080
  LoadBalancerPort:
    Type: Number
    Default: 8080
  HealthCheckPath:
    Type: String
    Default: /
  HostedZoneName:
    Type: String
    Default: labs.piratescloud.com
  1. On the Services search menu, click Cloud Formation.
  2. Click Create stack and With new resources (standard).
  3. For Prepare template select Choose an existing template.
  4. For Template source use Amazon S3 URL and paste url for cloudformation template:
https://lukado-public.s3.eu-west-1.amazonaws.com/labs/template-ecs-mario.yaml
  1. Click Next.
  2. Provide following informations:
    • Stack name: awsninjaX-stack
    • ServiceName: awsninjaX-sn
    • Subdomain: awsninja20X
    • VPC: Use your VPC
    • SubnetA: Select your Public1 subnet
    • SubnetB: Select your Public2 subnet
  3. Click Next.
  4. Scroll down to the Capabilities and mark selection for I acknowledge that AWS CloudFormation might create IAM resources with custom names.
  5. Click Next.
  6. Click Submit.

Wait until stack is in state CREATE_COMPLETE. You can observe the progress in Events - updated tab. You can choose Table view or newly added Timeline view.

In the meantime lets review resources defined in the template. Every resource is defined in Resources section.

Resources:

Below you can see a definition of the ECS Cluster

Resources:
  Cluster:
    Type: AWS::ECS::Cluster
    Properties:
      ClusterName: !Join ['', [!Ref ServiceName, Cluster]]

As you can see the Cluster name is defined as a combination of ServiceName and Cluster. ServiceName is a parameter defined in Parameters section.

For the name creation we use Join function. This function is used to join two or more strings together. In this case we join ServiceName and Cluster with empty string.

Additionall we can see that we use Ref function to get value of ServiceName parameter.

Bellow is a piece of code for TaskDefinition resource.

TaskDefinition:
    Type: AWS::ECS::TaskDefinition
    # Makes sure the log group is created before it is used.
    DependsOn: LogGroup
    Properties:
      # Name of the task definition. Subsequent versions of the task definition are grouped together under this name.
      Family: !Join ['', [!Ref ServiceName, TaskDefinition]]
      # awsvpc is required for Fargate
      NetworkMode: awsvpc
      RequiresCompatibilities:
        - FARGATE

Here you can find another parameter called DependsOn. This parameter is used to define dependencies between resources. In this case we define that TaskDefinition resource depends on LogGroup resource. This means that LogGroup resource will be created before TaskDefinition resource.

Let look at the Outputs section.

Outputs:
  Endpoint:
    Description: Endpoint
    Value: !Join ['', ['http://', !Ref DNSRecord,':8080']]

With the Outputs section we can define values that will be displayed after stack creation. In this case we define Endpoint value. This value is a combination of DNSRecord and 8080 port.

You can also use Outputs section to export values to other stacks. This is very useful when you want to use values from one stack in another stack.

OK, let go back to our stack creation. If the stack is in state CREATE_COMPLETE you can go to the next task.

Task 2. Review Events, Outpust, Resources etc.

Let try to examine our stack. We will look into Events, Outputs, Resources and Parameters sections.

  1. Find your stack and click on its name.
  2. Look into sections by selecting proper tab:

Resource

Here you can see all resources that were created during stack creation. You can also see their status and some additional information. You can use search field to find specific resource.

Events

Here you can see all events that were generated during stack creation. You can also see their status and some additional information. You can use search field to find specific event.

This is a good place to look for errors if something went wrong during stack creation. When something went wrong you should look here for the first event with status FAILED. This event will contain information about the error.

Parameters

Here you can see all parameters that were used during stack creation. You can also see their values. This is a good place to look for parameters values if you don't remember them, or need to use them in another stack.

Outputs

Here you can see all outputs that were defined in the template. You can also see their values.

  1. Click on Outputs section and click on the generated Endpoint url.
  2. Take the url and paste it into your browser.

Enjoy your game...😎

Task 3. Review deployed configuration

Let go back to the AWS Console and review deployed configuration.

  1. On the Services search menu, click ECS.
  2. Click on Clusters.
  3. Click on awsninjaX-cluster.
  4. Click on Tasks.

As you can see task is in RUNNING state. This means that our container is running on ECS Fargate cluster.

All of these resources were created by CloudFormation stack.

  1. On the Services search menu, click EC2.
  2. Click on Load Balancers.
  3. Try to find a load balancer created by CloudFormation stack.
  4. On the Services search menu, click Route53.
  5. Click on Hosted zones.
  6. Select your hosted zone.
  7. Try to find a record created by CloudFormation stack.

What type of record is it?

Task 3: Clean up

  1. Back in CloudFormation console select your stack by checking the checkmark left to its name.
  2. Click Delete.
  3. Click Delete stack.

lukado logo

END LAB

© 2026 lukado.eu