Skip to content

Instantly share code, notes, and snippets.

@MiyamotoTa
Created April 20, 2021 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MiyamotoTa/fe3a665f3cc47af1ace1c2ecb15a3c26 to your computer and use it in GitHub Desktop.
Save MiyamotoTa/fe3a665f3cc47af1ace1c2ecb15a3c26 to your computer and use it in GitHub Desktop.
variable "ami_name" {
type = string
default = "example-ami"
}
locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }
data "amazon-ami" "amzn2-arm64" {
filters = {
virtualization-type = "hvm"
name = "amzn2-ami-hvm-*-arm64-gp2"
}
owners = ["137112412989"]
most_recent = true
profile = "my-profile"
region = "ap-northeast-1"
}
variable "environment" {
type = string
default = "test"
}
source "amazon-ebs" "example" {
ami_name = "example ${local.timestamp}"
profile = "my-profile"
instance_type = "t4g.micro"
region = "ap-northeast-1"
source_ami = data.amazon-ami.amzn2-arm64.id
ssh_username = "ec2-user"
tags = {
"Name" = "example-${local.timestamp}"
"Environment" = "${var.environment}"
"Base_AMI_ID" = data.amazon-ami.amzn2-arm64.id
"Base_AMI_NAME" = data.amazon-ami.amzn2-arm64.name
}
}
build {
sources = ["source.amazon-ebs.example"]
provisioner "shell" {
inline = ["sleep 30", "sudo yum update -y"]
}
provisioner "ansible" {
playbook_file = "./ansible/playbooks/example.yml"
inventory_directory = "./ansible/inventory/${var.environment}"
user = "ec2-user"
}
provisioner "shell" {
inline = ["rm -rf /home/ec2-user/.ansible"]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment