0
votes

I'm trying out Terraform, and am in the process of translating one of my more interesting CloudFormation stacks to TF. Included as a key part of the stack is the following declaration that specifies a custom resource for the template - a Lambda that queries a list of AMIs and selects the latest one for the context, based on the description as a filter.

  LatestAMI:
    Type: Custom::LatestAMI
    Properties:
      ServiceToken: arn:aws:lambda:us-east-1:XXXXXXX:function:GetLatestAMI
      Description: ubuntu-16.04

I've looked around the Terraform docs, but I can't seem to find out how I can specify this resource. Is there a Terraform analog for custom resources in CloudFormation?

1

1 Answers

2
votes

The CF codes you posted calls a lambda function to get the latest ami id (filter with Description: ubuntu-16.04. There is simpler way to do in terraform.

You need data source aws_ami

https://www.terraform.io/docs/providers/aws/d/ami.html

Use this data source to get the ID of a registered AMI for use in other resources.

data "aws_ami" "latest_ami" {
  most_recent      = true
  executable_users = ["all"]

  filter {
    name   = "owner-alias"
    values = ["amazon"]
  }

  filter {
    name   = "name"
    values = ["*ubuntu-16.04*"]
  }
}