---
tags: ECS, Containers, AWS, Workshop
---
# ECS Workshop - Spot
 In this AWS workshop we will deploy a VPC Network and ECS Cluster with capacity providers to leverage Spot instance type.
## Clone the repository
```bash=
cd ~/environment
git clone https://github.com/awslabs/ec2-spot-workshops.git
cd ec2-spot-workshops/workshops/ecs-spot-capacity-providers
```
We are defining our deployment configuration via code using AWS Cloudformation. Let’s look through the code to better understand what resources CloudFormation will create.
## Update the CFN Stack
We first need to update the CFN template to latest schema standards, open `ec2-spot-workshops/workshops/ecs-spot-capacity-providers/ecs-spot-workshop-cfn.yaml` for editing in your IDE. (Cloud9)
Update the resource object `autoScalingServiceLinkedRole` starting on Line 276
```yaml=
autoScalingServiceLinkedRole:
Type: AWS::IAM::ServiceLinkedRole
Properties:
AWSServiceName: autoscaling.amazonaws.com
Description: Default Service-Linked Role enables access to AWS Services and Resources
used or managed by Auto Scaling
```
update shown below on line 5 to include a custom role suffix to no clash with other roles in this workshop studio account
```yaml=
autoScalingServiceLinkedRole:
Type: AWS::IAM::ServiceLinkedRole
Properties:
AWSServiceName: autoscaling.amazonaws.com
CustomSuffix: ecsworkshop
Description: Default Service-Linked Role enables access to AWS Services and Resources
used or managed by Auto Scaling
```
Save the file in Cloud9 and now we can run the CFN template
## Deploying CFN Stack
To start, we will deploy standard networking resources (VPC, Public and Private Subnets) using the following AWS CloudFormation (CFN) template, naming the stack as ecsworkshop-vpc
```bash=
aws cloudformation create-stack --stack-name=EcsSpotWorkshop --template-body file://ecs-spot-workshop-cfn.yaml --capabilities CAPABILITY_NAMED_IAM
```
Once the above CFN stack is ready (reached CREATE_COMPLETE state), the stack will export values namely VPCId, SecurityGroup , Public & Private SubnetIds. We will need these values when creating the EC2 instances. Run the following aws cli command which calls the DescribeStack API, and verifies the creation of networking resources
```bash=
aws cloudformation describe-stacks --stack-name EcsSpotWorkshop --query 'Stacks[*].Outputs' --output table
```
Resize the volume of the cloud9 environment
1. Select the Cloud9 instance in the EC2 console
2. Click the Storage : Section
3. Click on the Volume ID. That will take you to the EBS Volume page details
4. Modify the EBS volume.
5. Choose a new volume size (e.g. 100GB).

Changing the block device does not increase the size of the file system.
To do so head back to the Cloud9 instance and use the following commands to reboot the instance. It could take a minute or two for the IDE to come back online.
```bash=
sudo reboot
```
```bash=
df --human-readable
```
The output should show a volume with 100G size
```
Filesystem Size Used Avail Use% Mounted on
devtmpfs 960M 0 960M 0% /dev
tmpfs 978M 0 978M 0% /dev/shm
tmpfs 978M 452K 978M 1% /run
tmpfs 978M 0 978M 0% /sys/fs/cgroup
/dev/nvme0n1p1 100G 8.5G 92G 9% /
```
Install dependencies used in the workshop
```bash=
sudo yum -y install jq gettext
```
Change into the workshop directory
```bash=
cd ec2-spot-workshops/workshops/ecs-spot-capacity-providers
```
We should configure our aws cli with our current region as default
```bash=
export ACCOUNT_ID=$(aws sts get-caller-identity --output text --query Account)
export AWS_REGION=$(curl -s 169.254.169.254/latest/dynamic/instance-identity/document | jq -r '.region')
echo "export ACCOUNT_ID=${ACCOUNT_ID}" >> ~/.bash_profile
echo "export AWS_REGION=${AWS_REGION}" >> ~/.bash_profile
aws configure set default.region ${AWS_REGION}
aws configure get default.region
```
Use the commands below to set the CloudFormation stack name to an environment variable
```bash=
export STACK_NAME=EcsSpotWorkshop
```
```bash=
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
```
Follow the guide to run the workshop via:
https://ec2spotworkshops.com/ecs-spot-capacity-providers/module-1.html