diff --git a/internal/adapters/cloudformation/aws/ec2/adapt_test.go b/internal/adapters/cloudformation/aws/ec2/adapt_test.go index 39101e3..c87b084 100644 --- a/internal/adapters/cloudformation/aws/ec2/adapt_test.go +++ b/internal/adapters/cloudformation/aws/ec2/adapt_test.go @@ -79,6 +79,54 @@ Resources: ImageId: "ami-79fd7eee" LaunchTemplate: LaunchTemplateName: MyTemplate +`, + expected: ec2.EC2{ + LaunchTemplates: []ec2.LaunchTemplate{ + { + Metadata: types.NewTestMetadata(), + Name: types.String("MyTemplate", types.NewTestMetadata()), + Instance: ec2.Instance{ + Metadata: types.NewTestMetadata(), + MetadataOptions: ec2.MetadataOptions{ + HttpEndpoint: types.String("enabled", types.NewTestMetadata()), + HttpTokens: types.String("required", types.NewTestMetadata()), + }, + }, + }, + }, + Instances: []ec2.Instance{ + { + Metadata: types.NewTestMetadata(), + MetadataOptions: ec2.MetadataOptions{ + HttpEndpoint: types.String("enabled", types.NewTestMetadata()), + HttpTokens: types.String("required", types.NewTestMetadata()), + }, + RootBlockDevice: &ec2.BlockDevice{ + Metadata: types.NewTestMetadata(), + Encrypted: types.Bool(false, types.NewTestMetadata()), + }, + }, + }, + }, + }, + { + name: "ec2 instance with launch template, ref to id", + source: `AWSTemplateFormatVersion: 2010-09-09 +Resources: + MyLaunchTemplate: + Type: AWS::EC2::LaunchTemplate + Properties: + LaunchTemplateName: MyTemplate + LaunchTemplateData: + MetadataOptions: + HttpEndpoint: enabled + HttpTokens: required + MyEC2Instance: + Type: AWS::EC2::Instance + Properties: + ImageId: "ami-79fd7eee" + LaunchTemplate: + LaunchTemplateId: !Ref MyLaunchTemplate `, expected: ec2.EC2{ LaunchTemplates: []ec2.LaunchTemplate{ diff --git a/internal/adapters/cloudformation/aws/ec2/instance.go b/internal/adapters/cloudformation/aws/ec2/instance.go index 7bc08e0..1c2e0cf 100644 --- a/internal/adapters/cloudformation/aws/ec2/instance.go +++ b/internal/adapters/cloudformation/aws/ec2/instance.go @@ -22,7 +22,7 @@ func getInstances(ctx parser.FileContext) (instances []ec2.Instance) { UserData: r.GetStringProperty("UserData"), } - if launchTemplate := findRelatedLaunchTemplate(ctx, r); launchTemplate != nil { + if launchTemplate, ok := findRelatedLaunchTemplate(ctx, r); ok { instance = launchTemplate.Instance } @@ -48,22 +48,36 @@ func getInstances(ctx parser.FileContext) (instances []ec2.Instance) { return instances } -func findRelatedLaunchTemplate(fctx parser.FileContext, r *parser.Resource) *ec2.LaunchTemplate { +func findRelatedLaunchTemplate(fctx parser.FileContext, r *parser.Resource) (ec2.LaunchTemplate, bool) { launchTemplateRef := r.GetProperty("LaunchTemplate.LaunchTemplateName") + if launchTemplateRef.IsString() { + res := findLaunchTemplateByName(fctx, launchTemplateRef) + if res != nil { + return adaptLaunchTemplate(res), true + } + } - if !launchTemplateRef.IsString() || launchTemplateRef.IsEmpty() { - return nil + launchTemplateRef = r.GetProperty("LaunchTemplate.LaunchTemplateId") + if !launchTemplateRef.IsString() { + return ec2.LaunchTemplate{}, false } + resource := fctx.GetResourceByLogicalID(launchTemplateRef.AsString()) + if resource == nil { + return ec2.LaunchTemplate{}, false + } + return adaptLaunchTemplate(resource), true +} + +func findLaunchTemplateByName(fctx parser.FileContext, prop *parser.Property) *parser.Resource { for _, res := range fctx.GetResourcesByType("AWS::EC2::LaunchTemplate") { templateName := res.GetProperty("LaunchTemplateName") if templateName.IsNotString() { continue } - if launchTemplateRef.EqualTo(templateName.AsString()) { - launchTemplate := adaptLaunchTemplate(res) - return &launchTemplate + if prop.EqualTo(templateName.AsString()) { + return res } }