buildspec.yml

version: 0.2

env:
  variables:
    AWS_REGION: "AWS_REGION"
    ECR_REPO_NAME: "ECR_REPO_NAME"

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query "Account" --output text)
      - aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com
      - IMAGE_TAG=$(date -d "+9 hours" +%Y-%m-%d.%H.%M.%S)
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...
      - docker build -t $ECR_REPO_NAME:latest .
      - docker tag $ECR_REPO_NAME:latest $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPO_NAME:$IMAGE_TAG
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker images...
      - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPO_NAME:$IMAGE_TAG
      - echo Writing image definitions file...
      - echo "$(jq .containerDefinitions[].image=\\"$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPO_NAME:$IMAGE_TAG\\" taskdef.json)" > taskdef.json
artifacts:
  files:
    - appspec.yaml
    - taskdef.json

Dockerfile

FROM golang:alpine
WORKDIR /main
COPY main.go .
RUN apk add --no-cache curl && go mod init noah.io/ark/rest && go build main.go
EXPOSE 8080
CMD ["./main"]

taskdef.json

{
  "executionRoleArn": "arn:aws:iam::362708816803:role/skills-ecs-task-role",
  "containerDefinitions": [
    {
      "name": "skills-container",
      "image": "<IMAGE>",
      "essential": "true",
      "portMappings": [
        {
          "protocol": "tcp",
          "containerPort": 80
        }
      ],
      "healthCheck": {
        "retries": 3,
        "command": [
          "CMD-SHELL",
          "curl -f <http://localhost:80/health> || exit 1"
        ],
        "timeout": 5,
        "interval": 30,
        "startPeriod": null
      }
    }
  ],
  "requiresCompatibilities": ["EC2"],
  "cpu": "512",
  "memory": "512",
  "family": "skills-td"
}

Name : skills-build

스크린샷 2025-01-07 오전 10.28.34.png

소스 - Github 추가

스크린샷 2025-01-07 오전 10.29.17.png

Amazon Linux 선택 및 IAM Role Name : skills-build-role

스크린샷 2025-01-07 오전 10.29.48.png

도커 권한 추가