Skip to content

Commit

Permalink
Pick available instance type for Packer builds
Browse files Browse the repository at this point in the history
  • Loading branch information
brikis98 committed Feb 4, 2021
1 parent deda55a commit 033a20f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
5 changes: 3 additions & 2 deletions examples/packer-basic-example/build.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
"oci_base_image_ocid": "",
"oci_compartment_ocid": "",
"oci_pass_phrase": "",
"oci_subnet_ocid": ""
"oci_subnet_ocid": "",
"instance_type": "t2.micro"
},
"builders": [{
"type": "amazon-ebs",
"ami_name": "{{user `ami_base_name`}}-terratest-packer-example",
"ami_description": "An example of how to create a custom AMI on top of Ubuntu",
"instance_type": "t2.micro",
"instance_type": "{{user `instance_type`}}",
"region": "{{user `aws_region`}}",
"source_ami_filter": {
"filters": {
Expand Down
5 changes: 3 additions & 2 deletions examples/packer-docker-example/build.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"variables": {
"aws_region": "us-east-1",
"ami_name_base": "terratest-packer-docker-example"
"ami_name_base": "terratest-packer-docker-example",
"instance_type": "t2.micro"
},
"builders": [{
"name": "ubuntu-ami",
"ami_name": "{{user `ami_name_base`}}-{{isotime | clean_resource_name}}",
"ami_description": "An example of how to create a custom AMI with a simple web app on top of Ubuntu",
"instance_type": "t2.micro",
"instance_type": "{{user `instance_type`}}",
"region": "{{user `aws_region`}}",
"type": "amazon-ebs",
"source_ami_filter": {
Expand Down
15 changes: 13 additions & 2 deletions test/packer_basic_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func TestPackerBasicExample(t *testing.T) {
// Pick a random AWS region to test in. This helps ensure your code works in all regions.
awsRegion := terratest_aws.GetRandomStableRegion(t, nil, nil)

// Some AWS regions are missing certain instance types, so pick an available type based on the region we picked
instanceType := terratest_aws.GetRecommendedInstanceType(t, awsRegion, []string{"t2.micro", "t3.micro"})

// website::tag::1::Read Packer's template and set AWS Region variable.
packerOptions := &packer.Options{
// The path to where the Packer template is located
Expand All @@ -43,6 +46,7 @@ func TestPackerBasicExample(t *testing.T) {
Vars: map[string]string{
"aws_region": awsRegion,
"ami_base_name": fmt.Sprintf("%s", random.UniqueId()),
"instance_type": instanceType,
},

// Only build the AWS AMI
Expand Down Expand Up @@ -87,15 +91,18 @@ func TestPackerBasicExampleWithVarFile(t *testing.T) {
// Pick a random AWS region to test in. This helps ensure your code works in all regions.
awsRegion := terratest_aws.GetRandomStableRegion(t, nil, nil)

// Some AWS regions are missing certain instance types, so pick an available type based on the region we picked
instanceType := terratest_aws.GetRecommendedInstanceType(t, awsRegion, []string{"t2.micro", "t3.micro"})

// Create temporary packer variable file to store aws region
varFile, err := ioutil.TempFile("", "*.json")
require.NoError(t, err, "Did not expect temp file creation to cause error")

// Be sure to clean up temp file
defer os.Remove(varFile.Name())

// Write random generated aws region to temporary json file
varFileContent := []byte(fmt.Sprintf("{ \"aws_region\": \"%s\" }", awsRegion))
// Write the vars we need to a temporary json file
varFileContent := []byte(fmt.Sprintf(`{"aws_region": "%s", "instance_type": "%s"}`, awsRegion, instanceType))
_, err = varFile.Write(varFileContent)
require.NoError(t, err, "Did not expect writing to temp file %s to cause error", varFile.Name())

Expand Down Expand Up @@ -149,6 +156,9 @@ func TestPackerMultipleConcurrentAmis(t *testing.T) {
// Pick a random AWS region to test in. This helps ensure your code works in all regions.
awsRegion := terratest_aws.GetRandomStableRegion(t, nil, nil)

// Some AWS regions are missing certain instance types, so pick an available type based on the region we picked
instanceType := terratest_aws.GetRecommendedInstanceType(t, awsRegion, []string{"t2.micro", "t3.micro"})

packerOptions := &packer.Options{
// The path to where the Packer template is located
Template: "../examples/packer-basic-example/build.json",
Expand All @@ -157,6 +167,7 @@ func TestPackerMultipleConcurrentAmis(t *testing.T) {
Vars: map[string]string{
"aws_region": awsRegion,
"ami_base_name": fmt.Sprintf("%s", random.UniqueId()),
"instance_type": instanceType,
},

// Only build the AWS AMI
Expand Down
6 changes: 5 additions & 1 deletion test/terraform_packer_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func TestTerraformPackerExample(t *testing.T) {

// Build the AMI in packer-docker-example
func buildAMI(t *testing.T, awsRegion string, workingDir string) {
// Some AWS regions are missing certain instance types, so pick an available type based on the region we picked
instanceType := aws.GetRecommendedInstanceType(t, awsRegion, []string{"t2.micro", "t3.micro"})

packerOptions := &packer.Options{
// The path to where the Packer template is located
Template: "../examples/packer-docker-example/build.json",
Expand All @@ -75,7 +78,8 @@ func buildAMI(t *testing.T, awsRegion string, workingDir string) {

// Variables to pass to our Packer build using -var options
Vars: map[string]string{
"aws_region": awsRegion,
"aws_region": awsRegion,
"instance_type": instanceType,
},

// Configure retries for intermittent errors
Expand Down

0 comments on commit 033a20f

Please sign in to comment.