Skip to content

Instantly share code, notes, and snippets.

@erankitcs
Last active April 5, 2022 14:53
Show Gist options
  • Select an option

  • Save erankitcs/840594dfc0ec915fd4d8d6791f5acdef to your computer and use it in GitHub Desktop.

Select an option

Save erankitcs/840594dfc0ec915fd4d8d6791f5acdef to your computer and use it in GitHub Desktop.
Packer Image builder and CI/CD Pipeline
{
"variables": {
"aws_region": "us-east-1",
"base_ami": ""
},
"builders": [
{
"type": "amazon-ebs",
"region": "{{user `aws_region`}}",
"source_ami": "{{user `base_ami`}}",
"instance_type": "t2.micro",
"ssh_username": "ec2-user",
"ami_name": "webserver-image {{timestamp}}"
}
],
"provisioners": [
{
"type": "shell",
"script": "scripts/web_ami_setup.sh"
}
]
}
version: 0.2
phases:
pre_build:
commands:
- cd $CODEBUILD_SRC_DIR/ami_build_packer/
- echo "Installing Packer"
- curl -o packer.zip https://releases.hashicorp.com/packer/1.6.5/packer_1.6.5_linux_amd64.zip && unzip packer.zip
- echo "installed Packer."
build:
commands:
- ./packer build -color=false -var "base_ami=$BASE_AMI" images/web_ami_image.json | tee build.log
post_build:
commands:
- egrep "${AWS_REGION}\:\sami\-" build.log | cut -d' ' -f2 > ami_id.txt
# Packer doesn't return non-zero status; we must do that if Packer build failed
- test -s ami_id.txt || exit 1
- cat ami_id.txt
- aws ssm put-parameter --name $AMIID_SSMPS --type "String" --value $(cat ami_id.txt) --overwrite
- aws sns publish --topic-arn $SNS_ARN --message "Hi There... New AMI is available now. AMI ID- $(cat ami_id.txt)"
resource "aws_codepipeline" "amibuild_codepipeline" {
name = "tf-test-pipeline"
role_arn = aws_iam_role.codepipeline_role.arn
artifact_store {
location = aws_s3_bucket.amibuild_codepipeline_bucket.bucket
type = "S3"
}
stage {
name = "Source"
action {
name = "Source"
category = "Source"
owner = "ThirdParty"
provider = "GitHub"
version = "1"
output_artifacts = ["amibuild_artifacts"]
configuration = {
Owner = var.github_owner
Repo = data.github_repository.myrepo.name
Branch = "main"
OAuthToken = var.github_Oauthtoken
}
}
}
stage {
name = "Build"
action {
name = "Build"
category = "Build"
owner = "AWS"
provider = "CodeBuild"
input_artifacts = ["amibuild_artifacts"]
output_artifacts = ["amibuild_output"]
version = "1"
configuration = {
ProjectName = aws_codebuild_project.amibuid_codebuild.name
}
}
}
}
resource "aws_codebuild_project" "amibuid_codebuild" {
name = "amibuid_codebuild"
description = "AMI Build Codebuild pipeline"
build_timeout = "15"
service_role = aws_iam_role.codebuild_role.arn
artifacts {
type = "CODEPIPELINE"
}
cache {
type = "S3"
location = aws_s3_bucket.codebuild_log.bucket
}
environment {
compute_type = "BUILD_GENERAL1_SMALL"
image = "aws/codebuild/standard:1.0"
type = "LINUX_CONTAINER"
image_pull_credentials_type = "CODEBUILD"
environment_variable {
name = "AMIID_SSMPS"
value = var.ami_id_ssmps
}
environment_variable {
name = "SNS_ARN"
value = aws_sns_topic.amibuild_notification.arn
}
environment_variable {
name = "BASE_AMI"
value = var.base_ami_id
}
}
logs_config {
cloudwatch_logs {
group_name = "amibuild_log-group"
stream_name = "amibuild_log-stream"
}
s3_logs {
status = "ENABLED"
location = "${aws_s3_bucket.codebuild_log.id}/build-log"
}
}
source {
type = "CODEPIPELINE"
}
source_version = "master"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment