Skip to content

Commit be56c7e

Browse files
committed
ECS Labs
1 parent 78b55be commit be56c7e

36 files changed

+243
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: "ECR-ECS-Deployment"
2+
on:
3+
workflow_dispatch:
4+
5+
jobs:
6+
ECR_ECS_Deployment:
7+
name: Build Docker Image And Push To ECR And Deploy To ECS
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Checkout
11+
uses: actions/checkout@v2
12+
13+
- name: Setup AWS ECR Details
14+
uses: aws-actions/configure-aws-credentials@v1
15+
with:
16+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
17+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
18+
aws-region: ${{secrets.AWS_REGION}}
19+
20+
- name: Login to Amazon ECR
21+
id: login-pf-aws-ecr
22+
uses: aws-actions/amazon-ecr-login@v1
23+
24+
- name: Build and push the tagged docker image to Amazon ECR
25+
id : containerImage
26+
env:
27+
ECR_REGISTRY: ${{ steps.login-pf-aws-ecr.outputs.registry }}
28+
ECR_REPOSITORY: ${{secrets.AWS_ECR_REPO}}
29+
IMAGE_TAG: latest
30+
run: |
31+
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
32+
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
33+
34+
- name: ECR Status Check
35+
if: steps.containerImage.outcome == 'failure'
36+
run: exit 1
37+
38+
- name: Setup Terraform
39+
uses: hashicorp/[email protected]
40+
with:
41+
terraform_version: latest
42+
cli_config_credentials_token: ${{ secrets.TFC_API_TOKEN }}
43+
44+
- name: Terraform Init
45+
id: init
46+
run: terraform init
47+
48+
- name: Terraform Validate
49+
id: validate
50+
run: terraform validate
51+
52+
- name: Terraform Plan
53+
id: plan
54+
run: terraform plan
55+
continue-on-error: true
56+
57+
- name: Terraform Plan Status
58+
if: steps.plan.outcome == 'failure'
59+
run: exit 1
60+
61+
- name: Terraform Apply
62+
run: terraform apply -auto-approve

AWS_ECS_Labs/Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM python:3.10-alpine3.18
2+
3+
# WORKDIR /app
4+
5+
COPY . .
6+
7+
RUN pip install -r requirements.txt
8+
9+
EXPOSE 5000
10+
11+
ENV PYTHONUNBUFFERED=1
12+
13+
CMD ["python" ,"app.py"]

AWS_ECS_Labs/ECS/main.tf

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
3+
data "aws_vpc" "existing" {
4+
id = var.vpc_id
5+
}
6+
7+
8+
resource "aws_security_group" "ecs_sg" {
9+
vpc_id = data.aws_vpc.existing.id
10+
name = "ecs-security-group"
11+
# Inbound and outbound rules
12+
ingress {
13+
from_port = 5000
14+
to_port = 5000
15+
protocol = "tcp"
16+
cidr_blocks = ["0.0.0.0/0"]
17+
}
18+
egress {
19+
from_port = 0
20+
to_port = 0
21+
protocol = "-1"
22+
cidr_blocks = ["0.0.0.0/0"]
23+
}
24+
}
25+
26+
27+
resource "aws_ecs_task_definition" "task_definition" {
28+
family = var.cluster_service_task_name
29+
network_mode = "awsvpc"
30+
memory = "512"
31+
requires_compatibilities = ["FARGATE"]
32+
33+
34+
execution_role_arn = var.execution_role_arn
35+
36+
37+
container_definitions = jsonencode([
38+
{
39+
name = "flask-api-container"
40+
image = var.image_id
41+
cpu = 256
42+
memory = 512
43+
port_mappings = [
44+
{
45+
container_port = 5000
46+
host_port = 5000
47+
protocol = "tcp"
48+
}
49+
]
50+
}
51+
])
52+
53+
cpu = "256"
54+
}
55+
56+
57+
resource "aws_ecs_cluster" "ecs_cluster" {
58+
name = var.cluster_name
59+
}
60+
61+
resource "aws_ecs_service" "service" {
62+
name = var.cluster_service_name
63+
cluster = aws_ecs_cluster.ecs_cluster.id
64+
task_definition = aws_ecs_task_definition.task_definition.arn
65+
desired_count = 1
66+
launch_type = "FARGATE"
67+
68+
network_configuration {
69+
subnets = [var.vpc_id_subnet_list[0], var.vpc_id_subnet_list[1], var.vpc_id_subnet_list[2], var.vpc_id_subnet_list[3]]
70+
security_groups = [aws_security_group.ecs_sg.id]
71+
assign_public_ip = true
72+
}
73+
}

AWS_ECS_Labs/ECS/variables.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
3+
variable "vpc_id" {}
4+
5+
variable "cluster_name" {}
6+
7+
variable "cluster_service_name" {}
8+
9+
variable "cluster_service_task_name" {}
10+
11+
variable "image_id" {}
12+
13+
variable "vpc_id_subnet_list" {}
14+
15+
variable "execution_role_arn" {}
16+

AWS_ECS_Labs/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# AWS_ECS_Labs
2+
A lab session on Deploying Images to Amazon ECS via ECR

AWS_ECS_Labs/app.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from flask import Flask
2+
import random
3+
import time
4+
5+
app = Flask(__name__)
6+
7+
@app.route('/time')
8+
def get_current_time():
9+
timestamp = str(int(time.time()))
10+
response = {
11+
"time": timestamp,
12+
"message": "success",
13+
"httpsresponse": "200",
14+
"testedTIME": "yes"
15+
}
16+
return response
17+
18+
## Add a new route here
19+
@app.route('/random')
20+
def get_random_numbers():
21+
numbers = [random.randint(0, 5) for _ in range(10)]
22+
response = {
23+
"random_number": numbers,
24+
"message": "success",
25+
"httpsresponse": "200",
26+
"testedTIME": "yes"
27+
}
28+
return response
29+
30+
31+
if __name__ == '__main__':
32+
app.run(host='0.0.0.0', port=5000)

AWS_ECS_Labs/main.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
module "ecs" {
3+
source = "./ECS"
4+
vpc_id = "vpc-29568551"
5+
cluster_name = "demo-api-cluster"
6+
cluster_service_name = "cloudquicklabs-api-service"
7+
cluster_service_task_name = "cloudquicklabs-api-task"
8+
vpc_id_subnet_list = ["subnet-470f460c", " subnet-34b48b6e", "subnet-9b072be2", "subnet-f78ebadf"]
9+
execution_role_arn = "arn:aws:iam::357171621133:role/ETLlambdaAccessRole"
10+
image_id = "357171621133.dkr.ecr.us-west-2.amazonaws.com/ecsdemo:latest"
11+
}
12+
*/

AWS_ECS_Labs/provider.tf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
terraform {
2+
3+
required_providers {
4+
aws = {
5+
source = "hashicorp/aws"
6+
version = "~> 4.15.0"
7+
}
8+
9+
random = {
10+
source = "hashicorp/random"
11+
version = "3.1.0"
12+
}
13+
kubernetes = {
14+
source = "hashicorp/kubernetes"
15+
version = ">= 2.0.1"
16+
}
17+
}
18+
19+
backend "remote" {
20+
hostname = "app.terraform.io"
21+
organization = "CloudQuickLabs"
22+
23+
workspaces {
24+
name = "AWSECS"
25+
}
26+
}
27+
}
28+
29+
provider "aws" {
30+
region = "us-west-2"
31+
}
32+

AWS_ECS_Labs/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Flask>=1.1.1

AWS_LookOut_Vison/1-14.jpg

235 KB
Loading

0 commit comments

Comments
 (0)