You have permission to write objects to S3.
Operation confirmed with the following versions
bash:command:
aws --version
Result (example):
aws-cli/1.11.70 Python/2.7.12 Linux/4.4.11-23.53.amzn1.x86_64 botocore/1.5.33
If the version is old, update to the latest version.
bash:command:
sudo -H pip install -U awscli
First, check the variables.
bash:Check variables:
cat << ETX
AWS_DEFAULT_PROFILE: (0.1) ${AWS_DEFAULT_PROFILE}
AWS_DEFAULT_REGION: (0.2) ${AWS_DEFAULT_REGION}
S3_BUCKET_NAME (0.3) ${S3_BUCKET_NAME}
FILE_ZIP (0.4) ${FILE_ZIP}
SNS_TOPIC_NAME (0.5) ${SNS_TOPIC_NAME}
DIR_WORK (0.6) ${DIR_WORK}
PWD (0.7) $(pwd)
ETX
Result (example):
AWS_DEFAULT_PROFILE: (0.1)
If the variable is not included or is not appropriate, work on each step number I will do it.
Check the list of profiles.
bash:command:
cat ~/.aws/credentials \
| grep '\[' \
| sed 's/\[//g' | sed 's/\]//g'
Result (example):
iamFull-prjz-mbpr13
bash:Variable setting:
export AWS_DEFAULT_PROFILE='<Profile with permission to write objects to S3>'
bash:Variable setting:
export AWS_DEFAULT_REGION='ap-northeast-1'
bash:Variable setting:
S3_BUCKET_NAME=$( \
aws s3 ls \
| grep 'source-ap-northeast-1-' \
| cut -d ' ' -f3 \
) \
&& echo ${S3_BUCKET_NAME}
bash:Variable setting:
FILE_ZIP='MessageUtil.zip'
bash:Variable setting:
SNS_TOPIC_PREFIX='handson-'
bash:Variable setting:
SNS_TOPIC_ARN=$( \
aws sns list-topics \
--query "Topics[?contains(TopicArn, \`${SNS_TOPIC_PREFIX}\`)].TopicArn" \
--output text \
) \
&& echo ${SNS_TOPIC_ARN}
Result (example):
arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:handson-20170417
bash:Variable setting:
DIR_WORK='${HOME}/src-codebuild-demo-java'
bash:command:
cd ${DIR_WORK}/
bash:Check variables:
cat << ETX
AWS_DEFAULT_PROFILE: (0.1) ${AWS_DEFAULT_PROFILE}
AWS_DEFAULT_REGION: (0.2) ${AWS_DEFAULT_REGION}
S3_BUCKET_NAME (0.3) ${S3_BUCKET_NAME}
FILE_ZIP (0.4) ${FILE_ZIP}
SNS_TOPIC_NAME (0.5) ${SNS_TOPIC_NAME}
DIR_WORK (0.6) ${DIR_WORK}
PWD (0.7) $(pwd)
ETX
Result (example):
AWS_DEFAULT_PROFILE: (0.1)
bash:Variable setting:
SNS_MSG_SUBJECT='AWS CodeBuild - Build Completed'
bash:Variable setting:
SNS_MSG_BODY="${CODEB_PROJECT_NAME} build has completed." \
&& echo ${SNS_MSG_BODY}
bash:Variable setting:
FILE_INPUT="${DIR_WORK}/buildspec.yml"
bash:Check variables:
cat << ETX
FILE_INPUT: ${FILE_INPUT}
AWS_DEFAULT_REGION: ${AWS_DEFAULT_REGION}
SNS_TOPIC_ARN: ${SNS_TOPIC_ARN}
SNS_MSG_SUBJECT: ${SNS_MSG_SUBJECT}
SNS_MSG_BODY: ${SNS_MSG_BODY}
ETX
bash:command:
cat << EOF > ${FILE_INPUT}
version: 0.1
environment_variables:
plaintext:
AWS_DEFAULT_REGION: "${AWS_DEFAULT_REGION}"
SNS_TOPIC_ARN: "${SNS_TOPIC_ARN}"
SNS_MSG_SUBJECT: "${SNS_MSG_SUBJECT}"
SNS_MSG_BODY: "${SNS_MSG_BODY}"
phases:
install:
commands:
- echo Nothing to do in the install phase...
pre_build:
commands:
- echo Nothing to do in the pre_build phase...
build:
commands:
- echo Build started on \$(date +%Y-%m-%dZ%H:%M:%S)
- mvn install
post_build:
commands:
- echo Build completed on \$(date +%Y-%m-%dZ%H:%M:%S)
- aws sns publish --topic-arn \${SNS_TOPIC_ARN} --subject "\${SNS_MSG_SUBJECT}" --message "\${SNS_MSG_BODY}"
artifacts:
files:
- target/messageUtil-1.0.jar
EOF
cat ${FILE_INPUT}
bash:command:
PATH_ZIP="${HOME}/${FILE_ZIP}" \
&& echo ${PATH_ZIP}
bash:command:
zip -r ${PATH_ZIP} * -x '.*'
Result (example):
adding: buildspec.yml (deflated 56%)
adding: pom.xml (deflated 52%)
adding: src/ (stored 0%)
adding: src/test/ (stored 0%)
adding: src/test/java/ (stored 0%)
adding: src/test/java/TestMessageUtil.java (deflated 60%)
adding: src/main/ (stored 0%)
adding: src/main/java/ (stored 0%)
adding: src/main/java/MessageUtil.java (deflated 58%)
Check the contents of the created ZIP file.
bash:command:
unzip -Z ${PATH_ZIP}
Result (example):
Archive: /home/taro/MessageUtil.zip
Zip file size: 2305 bytes, number of entries: 9
-rw-rw-r-- 3.0 unx 418 tx defN 17-Apr-16 23:45 buildspec.yml
-rw-rw-r-- 3.0 unx 625 tx defN 17-Apr-16 12:37 pom.xml
drwxrwxr-x 3.0 unx 0 bx stor 17-Apr-16 12:36 src/
drwxrwxr-x 3.0 unx 0 bx stor 17-Apr-16 12:36 src/test/
drwxrwxr-x 3.0 unx 0 bx stor 17-Apr-16 12:37 src/test/java/
-rw-rw-r-- 3.0 unx 568 tx defN 17-Apr-16 12:37 src/test/java/TestMessageUtil.java
drwxrwxr-x 3.0 unx 0 bx stor 17-Apr-16 12:36 src/main/
drwxrwxr-x 3.0 unx 0 bx stor 17-Apr-16 12:37 src/main/java/
-rw-rw-r-- 3.0 unx 345 tx defN 17-Apr-16 12:37 src/main/java/MessageUtil.java
9 files, 1956 bytes uncompressed, 863 bytes compressed: 55.9%
Transfer the created ZIP file to the S3 bucket for the source file.
bash:command:
aws s3 cp ${PATH_ZIP} s3://${S3_BUCKET_NAME}/
Result (example):
upload: ../MessageUtil.zip to s3://source-ap-northeast-1-XXXXXXXXXXXX/MessageUtil.zip
bash:command:
aws s3 ls s3://${S3_BUCKET_NAME}/${FILE_ZIP}
Result (example):
2017-04-1701:23:45 3243 MessageUtil.zip
bash:command:
rm ${PATH_ZIP}