EC2용 보안 그룹 생성
CodeDeploy용 IAM Role 생성
ALB 생성 후 targe group 생성
/opt/ec2_launch.sh 에 제작.
원래는 alb target group register가 codedeploy에 의해 이루어져야 하나 잘 안돼서 register target을 ec2 생성할 때부터 함
#!/bin/bash
Id=$(aws ec2 run-instances \
--image-id ami-01cd604f66eeb80a1 \
--count 1 \
--instance-type t3.small \
--key-name wsi-keypair \
--security-group-ids sg-085c8edb70a0de357 \
--subnet-id subnet-02d837315e9db8ade \
--iam-instance-profile 'Arn=arn:aws:iam::492622758225:instance-profile/wsi-api' \ # iam role
--tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=$1},{Key=wsi:deploy:group,Value=dev-api}]" | jq -r .Instances[0].InstanceId)
echo $Id
while true
do
if aws elbv2 register-targets \
--target-group-arn arn:aws:elasticloadbalancing:ap-northeast-2:492622758225:targetgroup/wsi-api-tg/0729c5f32c4ab990 \
--targets Id=$Id ; then # target group registration
echo "alb target group register succeeded"
break
else
echo "alb target group register failed, please waiting register to target group"
fi
done
wsi-api-repo
├── scripts
│ ├── install_dependencies
│ ├── start_server
│ └── stop_server
├── src
│ └── app.py
├── appspec.yml
├── buildspec.yml
└── Dockerfile
#!/bin/bash
yum update -y
yum install -y docker
service docker start
usermod -aG docker ec2-user
chmod 777 /var/run/docker.sock
FILE=/home/ec2-user/.aws/config
if [ -f "$FILE" ]; then
echo "$FILE exists."
else
echo "$FILE does not exist. Crate directory"
mkdir /home/ec2-user/.aws
fi
cat << EOF > /home/ec2-user/.aws/config
[default]
region = ap-northeast-2
EOF
#!/bin/bash
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
$(aws ecr get-login --no-include-email)
aws ecr describe-images --repository-name wsi-api-ecr | docker pull 492622758225.dkr.ecr.ap-northeast-2.amazonaws.com/wsi-api-ecr:$(jq -r .imageDetails[0].imageTags[0])
aws ecr describe-images --repository-name wsi-api-ecr | docker run -d --name=app -p 80:80 492622758225.dkr.ecr.ap-northeast-2.amazonaws.com/wsi-api-ecr:$(jq -r .imageDetails[0].imageTags[0])
#!/bin/bash
isExistApp = `pgrep docker`
if [[ -n $isExistApp ]]; then
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
fi
FROM python:3.9-slim-buster
# set a directory for the app
WORKDIR /app
# copy all the src files to the container
COPY ./src .
# install dependencies
RUN pip install flask
# tell the port number the container should expose
EXPOSE 80
# run the command
CMD ["python", "./app.py"]
version: 0.0
os: linux
hooks:
BeforeInstall:
- location: scripts/install_dependencies
timeout: 300
runas: root
- location: scripts/start_server
timeout: 300
runas: ec2-user
ApplicationStop:
- location: scripts/stop_server
timeout: 300
runas: ec2-user
version: 0.2
artifacts:
files:
- appspec.yml
- scripts/*
-
phases:
pre_build:
commands:
- echo Logging in to Amazon ECR...
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
build:
commands:
- TZ='Asia/Seoul'
- IMAGE_TAG=$(date +"%y%m%d%H%M%S")
- echo Build started on $IMAGE_TAG
- echo Building the Docker image...
- docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG .
- docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG
- sed -i 's/\r$//' scripts/install_dependencies
- sed -i 's/\r$//' scripts/start_server
- sed -i 's/\r$//' scripts/stop_server
post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker image...
- docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG