Last active
November 24, 2019 11:26
-
-
Save gordonmurray/259eb3c52e66188ea4b0e3b420a6ccd8 to your computer and use it in GitHub Desktop.
Create an AWS ECS Cluster
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# The following steps will create an ECS cluster on AWS | |
# This process assumes you have an AWS account with the AWS CLI installed locally | |
# It will ask you which VPC and Subnets to use | |
# Show the commands being executed and exit if there is a problem | |
set -ex | |
# AWS Region to use | |
REGION=us-east-1 | |
# Key Pair name to create | |
KEY_PAIR_NAME=example | |
# EC2 optimized AMI | |
AMI_ID=ami-097e3d1cdb541f43e | |
# | |
# No need to change anything beyond this point | |
# | |
# List existing VPCs in the region | |
aws ec2 describe-vpcs --region ${REGION} --query 'Vpcs[*].VpcId' | |
# Read in the users VPC choice | |
echo "please chose the VPC to use:" | |
read VPC_ID | |
# Create a key pair and save a local copy as a pem file | |
aws ec2 create-key-pair --key-name ${KEY_PAIR_NAME} --region ${REGION} --query 'KeyMaterial' --output text > ${KEY_PAIR_NAME}.pem | |
# Create EC2 instance Security Group, it will return its security group ID | |
aws ec2 create-security-group --region ${REGION} --group-name instances-sg --description "For EC2 instances" | |
# Create an Application Load Balancer Security Group, it will return its security group ID | |
aws ec2 create-security-group --region ${REGION} --group-name alb-sg --description "For the ALBs" | |
echo "Please enter your Instance security group:" | |
read INSTANCE_SG | |
echo "Please enter your ALB security group:" | |
read ALB_SG | |
# List existing Subnets in the chosen VPC | |
aws ec2 describe-subnets --region ${REGION} --query 'Subnets[*].SubnetId' --filters "Name=vpc-id,Values=${VPC_ID}" | |
# Read in the users Subnet choice | |
echo "Please enter 2 different Subnet IDs to use from the above list:" | |
read SUBNET_ID_1 | |
read SUBNET_ID_2 | |
# Add rule to the security group to allow port 80 open to all | |
aws ec2 authorize-security-group-ingress --region ${REGION} --group-name instances-sg --to-port 80 --ip-protocol tcp --cidr-ip 0.0.0.0/0 --from-port 80 | |
aws ec2 authorize-security-group-ingress --region ${REGION} --group-name alb-sg --to-port 80 --ip-protocol tcp --cidr-ip 0.0.0.0/0 --from-port 80 | |
# Add rule to let the ALB SG in to the instances SG | |
aws ec2 authorize-security-group-ingress --region ${REGION} --group-name instances-sg --protocol tcp --port 1-65535 --source-group alb-sg | |
# Create an ALB with 2 Subnets | |
aws elbv2 create-load-balancer --region ${REGION} --name example-load-balancer --security-groups ${ALB_SG} --subnets ${SUBNET_ID_1} ${SUBNET_ID_2} | |
# Get the ARN of the ALB we just created | |
ALB_ARN=`aws elbv2 describe-load-balancers --region ${REGION} --names "example-load-balancer" --query 'LoadBalancers[*].[LoadBalancerArn]' --output text` | |
# Create a Target Group | |
aws elbv2 create-target-group --region ${REGION} --name example-targets --protocol HTTP --port 80 --target-type instance --vpc-id ${VPC_ID} | |
# Get the ARN of the Target group we just created | |
TG_ARN=`aws elbv2 describe-target-groups --region ${REGION} --names "example-targets" --query 'TargetGroups[*].[TargetGroupArn]' --output text` | |
# Create an ALB listener | |
aws elbv2 create-listener \ | |
--load-balancer-arn ${ALB_ARN} \ | |
--protocol HTTP \ | |
--port 80 \ | |
--default-actions Type=forward,TargetGroupArn=${TG_ARN} \ | |
--region ${REGION} | |
# Create the cluster | |
aws ecs create-cluster --cluster-name example --region ${REGION} | |
# Create container instance, this is just an EC2 instance that is part of an ECS Cluster and has docker and the ecs-agent running on it. | |
aws ec2 run-instances --image-id ${AMI_ID} --count 1 \ | |
--instance-type t2.micro --key-name ${KEY_PAIR_NAME} \ | |
--subnet-id ${SUBNET_ID_1} --security-group-ids ${INSTANCE_SG} \ | |
--iam-instance-profile Arn=arn:aws:iam::016230046494:instance-profile/ecsInstanceRole \ | |
--user-data file://data.txt \ | |
--region ${REGION} \ | |
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=example}]' | |
# Create Task Definition - Describes how a docker container should launch. It contains settings like ports, docker image, cpu, memory, command to run and env variables. | |
aws ecs register-task-definition --cli-input-json file://definition.json --region ${REGION} | |
# Update the service.json file to include the Target Group ARN | |
sed -i "s/TARGET_GROUP_ARN/${TG_ARN//\//\\/}/g" service.json.template > service.json | |
# Create the Service responsible for maintaining the tasks | |
aws ecs create-service --cli-input-json file://service.json --region ${REGION} | |
# Query the LB to get its public DNS | |
aws elbv2 describe-load-balancers --region ${REGION} --load-balancer-arns ${ALB_ARN}--query 'LoadBalancers[*].[DNSName]' --output text | |
# Open the DNSName in your web browser to see your running container output - might take a few seconds |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Remove the ECS cluster items | |
# This process assumes you have an AWS account with the AWS CLI installed locally | |
# Show the commands being executed and exit if there is a problem | |
set -ex | |
# AWS region | |
REGION="us-east-1" | |
# Key Pair name | |
KEY_PAIR_NAME=example | |
# Get the instance ID of the EC2 instance | |
INSTANCE_ID=`aws ec2 describe-instances --filter "Name=tag:Name,Values=example" --region ${REGION} --query 'Reservations[].Instances[].[InstanceId]' --output text` | |
# delete the EC2 instance | |
aws ec2 terminate-instances --region ${REGION} --instance-ids ${INSTANCE_ID} | |
# delete the ALB | |
ALB_ARN=`aws elbv2 describe-load-balancers --region ${REGION} --names "example-load-balancer" --query 'LoadBalancers[*].[LoadBalancerArn]' --output text` | |
ALB_LISTENER_ARN=`aws elbv2 describe-listeners --load-balancer-arn ${ALB_ARN} --query 'Listeners[*].[ListenerArn]' --region ${REGION} --output text` | |
aws elbv2 delete-listener --listener-arn ${ALB_LISTENER_ARN} --region ${REGION} | |
aws elbv2 delete-load-balancer --load-balancer-arn ${ALB_ARN} --region ${REGION} | |
# delete the Target Group | |
TG_ARN=`aws elbv2 describe-target-groups --region ${REGION} --names "example-targets" --query 'TargetGroups[*].[TargetGroupArn]' --output text` | |
aws elbv2 delete-target-group --target-group-arn ${TG_ARN} | |
# delete security group | |
aws ec2 delete-security-group --region ${REGION} --group-name instances-sg | |
aws ec2 delete-security-group --region ${REGION} --group-name alb-sg | |
# scale service to 0 | |
aws ecs update-service --cluster example --service my-service --desired-count 0 --region ${REGION} | |
# delete services | |
aws ecs delete-service --cluster example --service my-service | |
# delete the ecs cluster | |
aws ecs delete-cluster --cluster example --region ${REGION} | |
# delete the key pair from AWS | |
aws ec2 delete-key-pair --region ${REGION} --key-name ${KEY_PAIR_NAME} | |
# delete the key pair from the local folder | |
rm ${KEY_PAIR_NAME}.pem |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment