Giter Club home page Giter Club logo

terraform-aws-imagebuilder-pipeline's Introduction

terraform-aws-imagebuilder-pipeline

tflint tfsec yamllint misspell pre-commit-check follow on Twitter

Terraform module for creating EC2 Image Builder Pipelines

Example

Here's what using the module will look like. Note that this module needs at least one recipe and component to be useful. See examples for details.

module "test_pipeline" {
  source  = "rhythmictech/imagebuilder-pipeline/aws"

  description = "Testing pipeline"
  name        = "test-pipeline"
  recipe_arn  = module.test_recipe.recipe_arn
  public      = false
}

About

Allows the creation of EC2 Image Builder Pipelines

Build Scheduling

Builds are scheduled by a cron pattern. The pipeline takes a schedule argument as follows:

  schedule_cron = "cron(0 0 * * mon)"
  schedule_pipeline_execution_start_condition = "EXPRESSION_MATCH_AND_DEPENDENCY_UPDATES_AVAILABLE"
    

The default expects an upstream AMI as a parent image and will build weekly only if an updated image is found upstream. By setting schedule_pipeline_execution_start_condition = "EXPRESSION_MATCH_ONLY", the build pipeline will always run.

When scheduling linked jobs, it is important to be mindful of the cron schedules. If both pipelines run with schedule_cron = "cron(0 0 * * mon)", the downstream build will always run one week late. Due to the testing phase and startup/teardown time, even a short EC2 Image Builder process can take over 15 minutes to run end to end. Complex test suites can take much longer.

See Amazon's EC2 Image Builder API Reference for further details.

Providing Launch Template configurations

If you want to update launch configurations as part of the Image Build process, you can provide them with the launch_template_configurations variable. It accepts a map of regions, where each region is a list of launch template configuration maps (one per account) for that region. It will look like this:

  launch_template_configurations  = {
    "us-east-1" = [
      {
        launch_template_id = "lt-0f1aedef76c015126"
        account_id         = "123456789012"
      },
      {
        launch_template_id = "lt-0f1aedef86c049140"
        account_id         = "234567890123"
        default            = "false"
      }
    ]
    "us-west-1" = [
      {
        launch_template_id = "lt-0f1aedef76c015113"
        account_id         = "123456789012"
      }
    ]
  }

Note that you do not have to provide a launch template configuration for every account and region you build AMIs in. You will also need to set up IAM permissions in the destination accounts per https://docs.aws.amazon.com/imagebuilder/latest/userguide/cross-account-dist.html. (You will need to set similar permissions via additional_iam_policy_arns for your own image builder pipeline if it is writing to your own account)

Providing your own Distribution Configuration

By default this module will try to handle the aws_imagebuilder_distribution_configuration configuration by itself. This works for more simple builds that only need to create EC2 images, but it may not be suitable for all users. The custom_distribution_configs aims to handle this by allowing users to provide a list of distribution configuration blocks, based off of the terraform described at https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/imagebuilder_distribution_configuration#distribution. Where additional configuration blocks are present, they must be replaced with a map of the same name. An example of this is:

  custom_distribution_configs = [
    {
      region = "us-east-1",
      ami_distribution_configuration = {
        name = "example-build-{{ imagebuilder:buildDate }}"
        launch_permission = {
          user_ids = ["123456789012"]
        }
      }
      launch_template_configuration = {
        launch_template_id = "lt-0123456789abcde"
      }
    },
    {
      region = "us-west-1"
      ami_distribution_configuration = {
        name = "example-build-{{ imagebuilder:buildDate }}"
      }
      ...
    }
  ]

Requirements

Name Version
terraform >= 0.14
aws >= 4.22.0

Providers

Name Version
aws 4.66.0

Modules

No modules.

Resources

Name Type
aws_iam_instance_profile.this resource
aws_iam_policy.log_write resource
aws_iam_policy.secret_read resource
aws_iam_role.this resource
aws_iam_role_policy_attachment.additional resource
aws_iam_role_policy_attachment.core resource
aws_iam_role_policy_attachment.log_write resource
aws_iam_role_policy_attachment.secret_read resource
aws_imagebuilder_distribution_configuration.this resource
aws_imagebuilder_image_pipeline.this resource
aws_imagebuilder_infrastructure_configuration.this resource
aws_iam_policy_document.assume data source
aws_iam_policy_document.log_write data source
aws_iam_policy_document.secret_read data source
aws_secretsmanager_secret.ssh_key data source

Inputs

Name Description Type Default Required
additional_iam_policy_arns List of ARN policies for addional builder permissions list(string) [] no
container_recipe_arn ARN of the container recipe to use. Must change with Recipe version string null no
custom_distribution_configs To use your own distribution configurations for the ImageBuilder Distribution Configuration, supply a list of distribution configuration blocks as defined at https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/imagebuilder_distribution_configuration#distribution any [] no
description description of component string null no
enabled Whether pipeline is ENABLED or DISABLED bool true no
enhanced_image_metadata_enabled Whether additional information about the image being created is collected. Default is true. bool true no
image_name The name prefix given to the AMI created by the pipeline (a timestamp will be added to the end) string "" no
image_recipe_arn ARN of the image recipe to use. Must change with Recipe version string null no
image_tests_enabled Whether to run tests during image creation bool true no
image_tests_timeout_minutes Maximum time to allow for image tests to run number 60 no
instance_key_pair EC2 key pair to add to the default user on the builder string null no
instance_metadata_http_put_hop_limit The number of hops that an instance can traverse to reach its metadata. number null no
instance_metadata_http_tokens Whether a signed token is required for instance metadata retrieval requests. Valid values: required, optional. string "optional" no
instance_types Instance types to create images from. It's unclear why this is a list. Possibly because different types can result in different images (like ARM instances) list(string)
[
"t3.medium"
]
no
kms_key_id KMS Key ID to use when encrypting the distributed AMI, if applicable string null no
launch_template_configurations A map of regions, where each region is a list of launch template configuration maps (one per account) for that region. Not used when custom_distribution_configs is in use. any {} no
license_config_arns If you're using License Manager, your ARNs go here set(string) null no
log_bucket Bucket to store logs in. If this is ommited logs will not be stored string null no
log_prefix S3 prefix to store logs at. Recommended if sharing bucket with other pipelines string null no
name name to use for component string n/a yes
public Whether resulting AMI should be public bool false no
regions Regions that AMIs will be available in list(string)
[
"us-east-1",
"us-east-2",
"us-west-1",
"us-west-2",
"ca-central-1"
]
no
resource_tags Key-value map of tags to apply to resources created by this pipeline map(string) null no
schedule_cron Schedule (in cron) for when pipeline should run automatically https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-imagebuilder-imagepipeline-schedule.html string "" no
schedule_pipeline_execution_start_condition Start Condition Expression for when pipeline should run automatically https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-imagebuilder-imagepipeline-schedule.html string "EXPRESSION_MATCH_AND_DEPENDENCY_UPDATES_AVAILABLE" no
schedule_timezone Timezone (in IANA timezone format) that scheduled builds, as specified by schedule_cron, run on string "Etc/UTC" no
security_group_ids Security group IDs for the Image Builder list(string) null no
shared_account_ids AWS accounts to share AMIs with. If this is left null AMIs will be public set(string) [] no
shared_organization_arns Set of AWS Organization ARNs to allow access to the created AMI set(string) null no
shared_ou_arns Set of AWS Organizational Unit ARNs to allow access to the created AMI set(string) null no
sns_topic_arn SNS topic to notify when new images are created string null no
ssh_key_secret_arn If your ImageBuilder Components need to use an SSH Key (private repos, etc.), specify the ARN of the secretsmanager secret containing the SSH key to add access permissions (use arn OR name, not both) string null no
ssh_key_secret_name If your ImageBuilder Components need to use an SSH Key (private repos, etc.), specify the Name of the secretsmanager secret containing the SSH key to add access permissions (use arn OR name, not both) string null no
subnet Subnet ID to use for builder string null no
tags map of tags to use for component map(string) {} no
terminate_on_failure Change to false if you want to connect to a builder for debugging after failure bool true no

Outputs

Name Description
pipeline_arn ARN of EC2 Image Builder Pipeline
role_name The name of the IAM role for use if additional permissions are needed.

The Giants underneath this module

  • pre-commit.com/
  • terraform.io/
  • github.com/tfutils/tfenv
  • github.com/segmentio/terraform-docs

terraform-aws-imagebuilder-pipeline's People

Contributors

actions-user avatar cdaniluk avatar dgoodellrhy avatar pierrekerschgens avatar smiller171 avatar vermyndax avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

terraform-aws-imagebuilder-pipeline's Issues

[BUG] var.log_bucket is not accepting a bucket name

Describe the bug
Trying to apply a log_bucket var to the configuration. Turns out, this variable is required. I set this up, but alas:

Error: Invalid count argument

  on .terraform/modules/al2_base_image_pipeline/main.tf line 22, in data "aws_iam_policy_document" "log_write":
  22:   count = var.log_bucket != null ? 1 : 0

The "count" value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the count depends on.

To Reproduce
Steps to reproduce the behavior:

  1. Create a logging bucket with the configuration
  2. Supply the logging bucket name to var.log_bucket
  3. Terraform plan produces the above error.

Expected behavior
It should apply the logging bucket to the configuration.

Desktop (please complete the following information):

  • OS: macOS 13.2/Terraform 1.3.7
  • Browser Arc
  • Version 2.0.1

[Feature] Find a better solution for situations where a log_prefix is not provided for the S3 Bucket

Is your feature request related to a problem? Please describe.
If you don't specify a log_prefix for the S3 logging bucket, each time the pipeline runs AWS generates a new prefix based on the pipeline name and SSM Execution hash, and the end result is that your logs go into unpredictably names prefixes that aren't easy to sort by date, and are maybe impossible to identify unless you get an error message from the pipeline explaining where the logs were sent.

Describe the solution you'd like
I don't know, maybe just override an empty log_prefix with the pipeline name? Anything is better than the default behavior I think.

Describe alternatives you've considered
I mean, we're technically taking away functionality by forcing a prefix, in case there's anyone out there who wants to do things the bad way? We could leave it as is and rely on making logs more easy to access another way (cloudwatch? I don't know)

Additional context
you told me to make this issue in slack! so i did.

Automate TF deployment from: component > recipe> imagebuilder

I configured TF modules for (components and recipe) and pointed this module to the recipe module and it worked perfectly. Thanks for this project it's awesome. The question I have. Is it possible to automate the steps between component → recipe → imagebuilder-pipeline so I can just run it once instead of running the imagebuilder-pipeline module?

[BUG]The value supplied for parameter 'instanceProfileName' is not valid. The provided instance profile does not exist. Please specify a different instance profile and try again

Describe the bug
I got the following error from the embedded cloudformation stack: "The value supplied for parameter 'instanceProfileName' is not valid. The provided instance profile does not exist. Please specify a different instance profile and try again".

The stack name is the same as the name I assigned to the module. I checked the role created from Terraform and found the role is there. Then I used the aws cli command to check if the instance profile has been created. From the output I can see the instance profile has been created. Please see below for verification:

I see the principle for this role is ""Service": "ec2.amazonaws.com"", it looks like the cloudformation service is not listed in the principle, this may cause the cloudformation cannot find this role or instance profile. (correct me if i am wrong)

{
           "Path": "/",
           "InstanceProfileName": "jason-pipeline-imagebuilder-instance-profile-20220922071727921200000002",
           "InstanceProfileId": "AIPAWF2ABWU5GN24MBMYM",
           "Arn": "arn:aws:iam::xxxxxxxxxxx:instance-profile/jason-pipeline-imagebuilder-instance-profile-20220922071727921200000002",
           "CreateDate": "2022-09-22T07:17:27+00:00",
           "Roles": [
               {
                   "Path": "/",
                   "RoleName": "jason-pipeline-imagebuilder-role-20220922071727266500000001",
                   "RoleId": "AROAWF2ABWU5FOMOKJO32",
                   "Arn": "arn:aws:iam::xxxxxxxx:role/jason-pipeline-imagebuilder-role-20220922071727266500000001",
                   "CreateDate": "2022-09-22T07:17:27+00:00",
                   "AssumeRolePolicyDocument": {
                       "Version": "2012-10-17",
                       "Statement": [
                           {
                               "Sid": "",
                               "Effect": "Allow",


                               "Principal": {
                                   "Service": "ec2.amazonaws.com"
                               },


                               "Action": "sts:AssumeRole"
                           }
                       ]
                   }
               }
           ]
       },

To Reproduce
Steps to reproduce the behavior:
Easy to reproduce by using the code below:

module "imagebuilder-pipeline" {
  source  = "rhythmictech/imagebuilder-pipeline/aws"
  version = "0.5.2"

  # insert the 2 required variables here
  description = "Jason Testing pipeline"
  name        = "jason-pipeline"
 # tags        = local.tags
   tags = {
    created_on = "09212022"
  }
  recipe_arn  = module.test_recipe.recipe_arn
  public      = false

}

Expected behavior
all the stack finishes successfully and the image build pipeline created successfully.

Screenshots
see attachment

Desktop (please complete the following information):
using Terraform newest version

[BUG] The value supplied for parameter 'instanceProfileName' is not valid

Describe the bug
The latest module (v0.5.2) throws the following error:

"The value supplied for parameter 'instanceProfileName' is not valid. The provided instance profile does not exist.

The might be a race condition here as I can see the resource on a destroy:

  # aws_iam_instance_profile.this will be destroyed
  - resource "aws_iam_instance_profile" "this" {
      - arn         = "arn:aws:iam::123443211234:instance-profile/test-pipe-imagebuilder-instance-profile-20210914083450236900000002" -> null
      - create_date = "2021-09-14T08:34:52Z" -> null
      - id          = "test-pipe-imagebuilder-instance-profile-20210914083450236900000002" -> null
      - name        = "test-pipe-imagebuilder-instance-profile-20210914083450236900000002" -> null
      - name_prefix = "test-pipe-imagebuilder-instance-profile-" -> null
      - path        = "/" -> null
      - role        = "test-pijp-tg-imagebuilder-role-20210914083448508400000001" -> null
      - tags        = {} -> null
      - tags_all    = {} -> null
      - unique_id   = "AIPA3JAHVOPNQES7DJZNN" -> null
    }

There is probably something that I'm missing here. I can see the resource and as the name is a simple string, you'd suspect it to be valid.

To Reproduce
terraform apply

Full Stacktrace

│ Error: error waiting for CloudFormation Stack creation: failed to create CloudFormation stack, rollback requested (ROLLBACK_COMPLETE): ["The following resource(s) failed to create: [distConfig, infraConfig]. Rollback requested by user." "Resource creation cancelled" "Resource handler returned message: \"The value supplied for parameter 'instanceProfileName' is not valid. The provided instance profile does not exist. Please specify a different instance profile and try again. (Service: Imagebuilder, Status Code: 400, Request ID: 50600df7-a4fd-4eec-8b01-916d0405b38b, Extended Request ID: null)\" (RequestToken: c97e2aa0-68ca-0cb5-cc52-6bb03c098380, HandlerErrorCode: GeneralServiceException)"]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.