Skip to content

Use asg's launch template's image_id to directly resolve an upstream image from an ssm param #74

@nitrocode

Description

@nitrocode

Describe the Feature

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template#image_id-1

image_id - (Optional) The AMI from which to launch the instance or use a Systems Manager parameter convention e.g. resolve:ssm:parameter-name. See docs for more details.

The cloudposse asg module already supports this.

We should be able to modify this

data "aws_ssm_parameter" "ami" {
count = local.enabled ? 1 : 0
name = "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
}

and this

image_id = each.value["image_id"] == null ? join("", data.aws_ssm_parameter.ami[*].value) : each.value["image_id"]

with

variable "ssm_image_id" {
  default = "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
}
data "aws_ssm_parameter" "ami" {
  count = local.enabled ? 1 : 0
- name  = "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
+ name  = var.ssm_image_id
- image_id      = each.value["image_id"] == null ? join("", data.aws_ssm_parameter.ami[*].value) : each.value["image_id"]
+ image_id      = each.value["image_id"] == null ? "resolve:ssm:${var.ssm_image_id}" : each.value["image_id"]

Expected Behavior

Use resolve:ssm directly in the launch template

Use Case

This allows us to never have to update the AMI again since the launch template will always use the latest that resolves in the upstream SSM parameter value

Describe Ideal Solution

Described above

Alternatives Considered

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions