From 92d1b75228517235de011e5db860111f7f17a377 Mon Sep 17 00:00:00 2001 From: akmhmgc Date: Sat, 31 Aug 2024 09:46:22 +0900 Subject: [PATCH 1/3] add task build and push image --- Taskfile.yml | 11 +++++++++++ ecs.tf | 10 +++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Taskfile.yml b/Taskfile.yml index 23751e6..9fd3728 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -8,3 +8,14 @@ tasks: - | aws cloudtrail lookup-events --lookup-attributes AttributeKey=ResourceName,AttributeValue={{.ATTRIBUTES}} --max-items 1 \ | jq -r '.Events[] | .CloudTrailEvent = (.CloudTrailEvent | fromjson ) | select(.EventName == "UpdateService") | {EventTime: .EventTime, EventName: .EventName, Deployments: .CloudTrailEvent.responseElements.service.deployments.[] | {Status: .status, TaskDefinition: .taskDefinition}}' + build_and_push: + desc: "DockerイメージをビルドしてECRにプッシュする" + vars: + PROJECT_NAME: "hamaguchi/nginx" + ECR_REPOSITORY: "533557086642.dkr.ecr.ap-northeast-1.amazonaws.com/{{.PROJECT_NAME}}" + IMAGE_TAG: "latest" + cmds: + - aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin {{.ECR_REPOSITORY}} + - docker build --build-arg project=app --platform linux/amd64 -t {{.PROJECT_NAME}} . + - docker tag {{.PROJECT_NAME}}:latest {{.ECR_REPOSITORY}}:{{.IMAGE_TAG}} + - docker push {{.ECR_REPOSITORY}}:{{.IMAGE_TAG}} diff --git a/ecs.tf b/ecs.tf index 4e90aaf..90a43b7 100644 --- a/ecs.tf +++ b/ecs.tf @@ -30,7 +30,7 @@ resource "aws_ecs_task_definition" "nginx_task" { container_definitions = jsonencode([ { name = "nginx" - image = "public.ecr.aws/nginx/nginx:latest" + image = "${aws_ecr_repository.nginx.repository_url}:latest" portMappings = [ { containerPort = 80 @@ -97,3 +97,11 @@ resource "aws_ecr_pull_through_cache_rule" "ecr_public" { ecr_repository_prefix = "ecr-public" upstream_registry_url = "public.ecr.aws" } + +resource "aws_ecr_repository" "nginx" { + name = "hamaguchi/nginx" + + image_scanning_configuration { + scan_on_push = true + } +} From a6f6f2a1797fbe1f9ebb4b4e72290846f12a0089 Mon Sep 17 00:00:00 2001 From: akmhmgc Date: Sat, 31 Aug 2024 10:54:42 +0900 Subject: [PATCH 2/3] stop update service depending on task definition change --- ecs.tf | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ecs.tf b/ecs.tf index 90a43b7..d349fc8 100644 --- a/ecs.tf +++ b/ecs.tf @@ -85,6 +85,9 @@ resource "aws_ecs_service" "nginx_service" { container_name = "nginx" container_port = 80 } + lifecycle { + ignore_changes = [task_definition] + } } resource "aws_cloudwatch_log_group" "nginx_task" { From f250d38ba685cf6f800bb50490d3cbe70ee438cf Mon Sep 17 00:00:00 2001 From: akmhmgc Date: Sat, 31 Aug 2024 11:02:22 +0900 Subject: [PATCH 3/3] add docker image --- Dockerfile | 2 ++ dist/index.html | 10 ++++++++++ 2 files changed, 12 insertions(+) create mode 100644 Dockerfile create mode 100644 dist/index.html diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ff4a997 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,2 @@ +FROM nginx +COPY ./dist /usr/share/nginx/html diff --git a/dist/index.html b/dist/index.html new file mode 100644 index 0000000..9cc6691 --- /dev/null +++ b/dist/index.html @@ -0,0 +1,10 @@ + + + + + Document + + +

revision:32

+ +