From b84996678a39881b007ae926569be0f652dcdc4a Mon Sep 17 00:00:00 2001 From: lolli001 Date: Wed, 24 Jul 2024 15:37:01 -0400 Subject: [PATCH] Updated --- build.pkr.hcl | 13 +++++++------ buildspec.yml | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/build.pkr.hcl b/build.pkr.hcl index e7a135c..7fdfa66 100644 --- a/build.pkr.hcl +++ b/build.pkr.hcl @@ -8,12 +8,12 @@ variable "ansible_dir" { } source "amazon-ebs" "rhel" { - ami_name = "${var.project_name}-{{timestamp}}" - instance_type = "t3.micro" # Ensure the instance type is ARM64 compatible - region = "us-gov-west-1" - source_ami = "ami-03f5c6b8195c66b04" # Updated AMI ID for RHEL 9 ARM - ssh_username = "ec2-user" - kms_key_id = "ba7dabdd-0f5a-44aa-9a90-a36c246a29a4" # Static KMS Key ID + ami_name = "${var.project_name}-{{timestamp}}" + instance_type = "t3.micro" # Ensure the instance type is ARM64 compatible + region = "us-gov-west-1" + source_ami = "ami-03f5c6b8195c66b04" # Updated AMI ID for RHEL 9 ARM + ssh_username = "ec2-user" + kms_key_id = "ba7dabdd-0f5a-44aa-9a90-a36c246a29a4" # Static KMS Key ID } build { @@ -26,6 +26,7 @@ build { post-processor "shell-local" { inline_shebang = "/bin/sh" inline = [ + "echo 'Build finished!'", "echo '${build.amis}' > ami_id.txt", ] } diff --git a/buildspec.yml b/buildspec.yml index 4e3a192..fd6792c 100644 --- a/buildspec.yml +++ b/buildspec.yml @@ -1,10 +1,25 @@ version: 0.2 +env: + variables: + AWS_REGION: "us-gov-west-1" + CODE_SRC_DIR: "source" + CODEBUILD_SRC_DIR_SourceAnsibleOutput: "/codebuild/output/src3467/src/s3/01" + phases: install: runtime-versions: python: 3.x commands: + - echo "Installing Python version 3.11 ..." + - pyenv global $PYTHON_311_VERSION + - stat ${CODEBUILD_SRC_DIR_SourceAnsibleOutput}/init.sh && bash ${CODEBUILD_SRC_DIR_SourceAnsibleOutput}/init.sh ${CODEBUILD_SRC_DIR_SourceAnsibleOutput} || echo "No init.sh script found" + - which pip3 || curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py + - which pip3 || python3 get-pip.py --user + - stat $(python3 -m site --user-base)/bin/ansible-playbook || python3 -m pip install --user ansible pywinrm + - stat /bin/packer || curl -s -qL -o packer.zip https://releases.hashicorp.com/packer/1.10.3/packer_1.10.3_linux_amd64.zip + - stat /bin/packer || unzip -o packer.zip + - stat /bin/packer || $(mv packer /bin && rm packer.zip) - echo "Installing pip, Ansible, and Packer" - which pip3 || curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python3 get-pip.py --user - stat $(python3 -m site --user-base)/bin/ansible-playbook || python3 -m pip install --user ansible pywinrm @@ -18,6 +33,10 @@ phases: pre_build: commands: + - echo "Configuring AWS credentials" + - echo "Fetching AWS credentials from ECS metadata service" + - echo "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI=${AWS_CONTAINER_CREDENTIALS_RELATIVE_URI}" + - curl -qL -o aws_credentials.json http://169.254.170.2/${AWS_CONTAINER_CREDENTIALS_RELATIVE_URI} > aws_credentials.json - echo "Configuring AWS CLI" - aws configure set region $AWS_REGION - aws configure set aws_access_key_id `jq -r '.AccessKeyId' aws_credentials.json` @@ -29,15 +48,23 @@ phases: build: commands: + - echo "Building HashiCorp Packer template, build.pkr.hcl" + - cd ${CODEBUILD_SRC_DIR}/${CODE_SRC_DIR} + - /bin/packer init build.pkr.hcl + - ln -s ${CODEBUILD_SRC_DIR_SourceAnsibleOutput}/roles + - /bin/packer build -var project_name=rhel-arm-image-pipeline-demo -var ansible_dir=${CODEBUILD_SRC_DIR_SourceAnsibleOutput} build.pkr.hcl - echo "Running Packer build" - packer init build.pkr.hcl - packer build -var project_name=rhel-arm-image-pipeline-demo -var ansible_dir=${CODEBUILD_SRC_DIR_SourceAnsibleOutput} build.pkr.hcl post_build: commands: + - cd ${CODEBUILD_SRC_DIR}/${CODE_SRC_DIR} - echo "Checking for the AMI ID file" - test -s ami_id.txt || exit 1 artifacts: files: + - '**/*' + discard-paths: yes - ami_id.txt \ No newline at end of file