Skip to content

Commit

Permalink
[define the ecs container in a yaml file]
Browse files Browse the repository at this point in the history
  • Loading branch information
alvo254 committed Jun 10, 2024
1 parent 1661571 commit 184d5d5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 51 deletions.
39 changes: 11 additions & 28 deletions .github/workflows/aws.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@
# and best practices on handling the access key credentials.



name: Deploy to Amazon ECS

on:
push:
branches: [ "main" ]

env:
AWS_REGION: us-east-1 # set this to your preferred AWS region, e.g., us-west-1
DOCKER_USERNAME: alvin254 # set this to your Docker Hub username
DOCKER_REPOSITORY: fitnesshero # set this to your Docker Hub repository name
ECS_SERVICE: app-service # set this to your Amazon ECS service name
ECS_CLUSTER: cloudific # set this to your Amazon ECS cluster name
CONTAINER_NAME: fitnesshero # set this to the name of the container in your task definition
AWS_REGION: us-east-1
DOCKER_USERNAME: alvin254
DOCKER_REPOSITORY: alvin254/fitnesshero
ECS_SERVICE: app-service
ECS_CLUSTER: cloudific
ECS_TASK_DEFINITION: modules/ecs/container_definitions.yaml
CONTAINER_NAME: app

permissions:
contents: read
Expand All @@ -53,23 +53,6 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 1.8.5


- name: Terraform Init
run: terraform init

- name: Terraform Apply
id: apply
run: terraform apply -auto-approve

- name: Get ECS Task Definition ARN
id: ecs-task-def
run: echo "ECS_TASK_DEFINITION=$(terraform output -raw ecs_task_definition_arn)" >> $GITHUB_ENV

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
Expand All @@ -84,17 +67,17 @@ jobs:
env:
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t $DOCKER_USERNAME/$DOCKER_REPOSITORY:$IMAGE_TAG -f frontend/Dockerfile .
docker push $DOCKER_USERNAME/$DOCKER_REPOSITORY:$IMAGE_TAG
echo "image=$DOCKER_USERNAME/$DOCKER_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
docker build -t $DOCKER_REPOSITORY:$IMAGE_TAG -f frontend/Dockerfile .
docker push $DOCKER_REPOSITORY:$IMAGE_TAG
echo "image=$DOCKER_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: ${{ env.ECS_TASK_DEFINITION }}
container-name: ${{ env.CONTAINER_NAME }}
image: $DOCKER_USERNAME/$DOCKER_REPOSITORY:$IMAGE_TAG
image: $DOCKER_REPOSITORY:$IMAGE_TAG

- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
Expand Down
8 changes: 4 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module "ecs" {
# container_image = "fitnesshero"
}

output "ecs_task_definition_arn" {
description = "The ARN of the ECS task definition from the module"
value = module.ecs.task_definition_arn
}
# output "ecs_task_definition_arn" {
# description = "The ARN of the ECS task definition from the module"
# value = module.ecs.task_definition_arn
# }
16 changes: 16 additions & 0 deletions modules/ecs/container_definitions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"name": "app",
"image": "fitnesshero",
"cpu": 256,
"memory": 512,
"essential": true,
"portMappings": [
{
"containerPort": 3000,
"hostPort": 3000,
"protocol": "tcp"
}
]
}
]
17 changes: 1 addition & 16 deletions modules/ecs/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,7 @@ resource "aws_ecs_task_definition" "cloudific" {
execution_role_arn = aws_iam_role.ecs_exec_role.arn
cpu = "256" // CPU value defined at task level
memory = "512" // Memory value defined at task level
container_definitions = jsonencode([
{
name = "app"
image = "fitnesshero"
cpu = 256
memory = 512
essential = true
portMappings = [
{
containerPort = 3000
hostPort = 3000
protocol = "tcp"
}
]
}
])
container_definitions = file("${path.module}/container_definitions.yaml")
}

resource "aws_ecs_service" "app_service" {
Expand Down
7 changes: 4 additions & 3 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
variable "container_image" {
type = string
}
# variable "container_image" {
# type = string
# }

# variable "access_key" {
# type = string
# }
Expand Down

0 comments on commit 184d5d5

Please sign in to comment.