... On your own

Deploying the CloudFormation stack

As a first step, download a CloudFormation stack that will deploy for you the following resources:

  • A VPC
  • An S3 bucket
  • An ECR repository
  • A Launch Template
  • An AWS Step Functions state machine
  • An instance profile for AWS Batch compute environment
  • The Cloud9 environment where you will run all the commands

After downloading the template, open the CloudFormation console and on the top-right corner of the screen, click on Create stack. Follow the following steps:

  1. In the Create stack page, click on Choose file and upload the CloudFormation template you just downloaded. Don’t change any other configuration parameter.
  2. In the Specify stack details page, set the stack name as MonteCarloWithBatch.
  3. In the Configure stack options page, leave all the configuration as it is. Navigate to the bottom of the page and click on Next.
  4. In the Review page, leave all the configuration as it is. Navigate to the bottom of the page, and click on I acknowledge that AWS CloudFormation might create IAM resources and finally on Create stack.

It is important that you use MonteCarloWithBatch as the stack name, as later we will use that value to retrieve some outputs programmatically.

The stack creation process will begin. All the resources will be ready to use when the status of the stack is CREATE_COMPLETE.

Reviewing the Launch Template

You can check the CloudFormation stack by downloading the following file: CloudFormation stack

Note that the UserData of the created Launch Template contains the following script:

MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="==MYBOUNDARY=="

--==MYBOUNDARY==
Content-Type: text/x-shellscript; charset="us-ascii"

#!/bin/bash
echo "ECS_CLUSTER=EcsSpotWorkshop" >> /etc/ecs/ecs.config
echo "ECS_ENABLE_SPOT_INSTANCE_DRAINING=true" >> /etc/ecs/ecs.config
echo "ECS_CONTAINER_STOP_TIMEOUT=90s" >> /etc/ecs/ecs.config
echo "ECS_ENABLE_CONTAINER_METADATA=true" >> /etc/ecs/ecs.config

--==MYBOUNDARY==--

What we are doing here is enabling Spot Instance Draining. When ECS Spot Instance draining is enabled on the instance, ECS receives the Spot Instance interruption notice and places the instance in DRAINING status. When a container instance is set to DRAINING, Amazon ECS prevents new tasks from being scheduled for placement on the container instance. To learn more about Spot instance interruption notices, visit Spot Instance interruption notices. For more information on how AWS Batch uses Amazon ECS, see the FAQs.

Gathering the CloudFormation outputs

You will create other AWS resources using the AWS CLI in Cloud9, a cloud-based integrated development environment (IDE) that lets you write, run, and debug your code with just a browser. It includes a code editor, debugger, and terminal. Cloud9 comes prepackaged with essential tools for popular programming languages, including JavaScript, Python, PHP, and more.

Navigate to the Cloud9 console and open the environment that was created for you. Execute the following commands to retrieve the outputs of the CloudFormation stack:

for output in $(aws cloudformation describe-stacks --stack-name ${STACK_NAME} --query 'Stacks[].Outputs[].OutputKey' --output text)
do
    export $output=$(aws cloudformation describe-stacks --stack-name ${STACK_NAME} --query 'Stacks[].Outputs[?OutputKey==`'$output'`].OutputValue' --output text)
    eval "echo $output : \"\$$output\""
done

You can now start the workshop by heading to Risk pipeline.