$BUCKET_NAME = (aws s3api list-buckets --query "Buckets[].Name" --output text) -split "`t" | Where-Object { $_ -match '^apdev-cloud-bucket-[a-zA-Z0-9]{4}$' }

aws s3 cp ./app s3://$BUCKET_NAME/app --recursive
BUCKET_NAME=$(aws s3api list-buckets --query "Buckets[].Name" --output text | tr '\\t' '\\n' | grep -E '^apdev-cloud-bucket-[a-zA-Z0-9]{4}$')

mkdir -p app

aws s3 cp s3://$BUCKET_NAME/app ./app --recursive
cat << 'EOF' > app/userv2/Dockerfile
FROM alpine:latest
WORKDIR /app

RUN apk add libc6-compat --no-cache  
RUN apk add curl tcpdump ngrep --no-cache

COPY userv2 .
RUN chmod +x userv2
CMD "./userv2"
EOF

cat << 'EOF' > app/stressv2/Dockerfile
FROM alpine:latest
WORKDIR /app

RUN apk add libc6-compat --no-cache  
RUN apk add curl tcpdump ngrep --no-cache

COPY stressv2 .
RUN chmod +x stressv2
CMD "./stressv2"
EOF

cat << 'EOF' > app/productv2/Dockerfile
FROM alpine:latest
WORKDIR /app

RUN apk add libc6-compat --no-cache  
RUN apk add curl tcpdump ngrep --no-cache

COPY productv2 .
RUN chmod +x productv2
CMD "./productv2"
EOF
mv app/userv2/user* app/userv2/userv2
mv app/stressv2/stress* app/stressv2/stressv2
mv app/productv2/product* app/productv2/productv2
mv app/userv2/user_1.0.1 app/userv2/userv2
mv app/stressv2/stress_1.0.1 app/stressv2/stressv2
mv app/productv2/product_1.0.1 app/productv2/productv2
cd app
ACCOUNT_ID=$(aws sts get-caller-identity --query "Account" --output text)
REGION_CODE=$(aws configure set region ap-northeast-2 && aws configure get region)

aws ecr get-login-password --region $REGION_CODE | docker login --username AWS --password-stdin $ACCOUNT_ID.dkr.ecr.$REGION_CODE.amazonaws.com
docker build -t $ACCOUNT_ID.dkr.ecr.$REGION_CODE.amazonaws.com/user:v1.0.1 ./userv2/
docker push $ACCOUNT_ID.dkr.ecr.$REGION_CODE.amazonaws.com/user:v1.0.1
cd app
ACCOUNT_ID=$(aws sts get-caller-identity --query "Account" --output text)
REGION_CODE=$(aws configure set region ap-northeast-2 && aws configure get region)

aws ecr get-login-password --region $REGION_CODE | docker login --username AWS --password-stdin $ACCOUNT_ID.dkr.ecr.$REGION_CODE.amazonaws.com
docker build -t $ACCOUNT_ID.dkr.ecr.$REGION_CODE.amazonaws.com/stress:v1.0.1 ./stressv2/
docker push $ACCOUNT_ID.dkr.ecr.$REGION_CODE.amazonaws.com/stress:v1.0.1
cd app
ACCOUNT_ID=$(aws sts get-caller-identity --query "Account" --output text)
REGION_CODE=$(aws configure set region ap-northeast-2 && aws configure get region)

aws ecr get-login-password --region $REGION_CODE | docker login --username AWS --password-stdin $ACCOUNT_ID.dkr.ecr.$REGION_CODE.amazonaws.com
docker build -t $ACCOUNT_ID.dkr.ecr.$REGION_CODE.amazonaws.com/product:v1.0.1 ./productv2/
docker push $ACCOUNT_ID.dkr.ecr.$REGION_CODE.amazonaws.com/product:v1.0.1