Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip ebs encryption flag for snapshots on launch template. #4620

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions aws/resource_aws_launch_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,25 @@ func buildLaunchTemplateData(d *schema.ResourceData, meta interface{}) (*ec2.Req
UserData: aws.String(d.Get("user_data").(string)),
}

conn := meta.(*AWSClient).ec2conn
imagesOutput, err := conn.DescribeImages(&ec2.DescribeImagesInput{
ImageIds: []*string{
aws.String(d.Get("image_id").(string)),
},
})
if err != nil {
return nil, err
}
image := imagesOutput.Images[0]
snapshotMappings := map[string]bool{}
for _, mapping := range image.BlockDeviceMappings {
if mapping.Ebs != nil {
if mapping.Ebs.SnapshotId != nil {
snapshotMappings[*mapping.DeviceName] = true
}
}
}

if v, ok := d.GetOk("image_id"); ok {
opts.ImageId = aws.String(v.(string))
}
Expand Down Expand Up @@ -803,7 +822,7 @@ func buildLaunchTemplateData(d *schema.ResourceData, meta interface{}) (*ec2.Req
bdms := v.([]interface{})

for _, bdm := range bdms {
blockDeviceMappings = append(blockDeviceMappings, readBlockDeviceMappingFromConfig(bdm.(map[string]interface{})))
blockDeviceMappings = append(blockDeviceMappings, readBlockDeviceMappingFromConfig(bdm.(map[string]interface{}), snapshotMappings))
}
opts.BlockDeviceMappings = blockDeviceMappings
}
Expand Down Expand Up @@ -896,7 +915,7 @@ func buildLaunchTemplateData(d *schema.ResourceData, meta interface{}) (*ec2.Req
return opts, nil
}

func readBlockDeviceMappingFromConfig(bdm map[string]interface{}) *ec2.LaunchTemplateBlockDeviceMappingRequest {
func readBlockDeviceMappingFromConfig(bdm map[string]interface{}, snapshotMappings map[string]bool) *ec2.LaunchTemplateBlockDeviceMappingRequest {
blockDeviceMapping := &ec2.LaunchTemplateBlockDeviceMappingRequest{}

if v := bdm["device_name"].(string); v != "" {
Expand All @@ -915,22 +934,24 @@ func readBlockDeviceMappingFromConfig(bdm map[string]interface{}) *ec2.LaunchTem
ebs := v.([]interface{})
if len(ebs) > 0 {
ebsData := ebs[0]
blockDeviceMapping.Ebs = readEbsBlockDeviceFromConfig(ebsData.(map[string]interface{}))
blockDeviceMapping.Ebs = readEbsBlockDeviceFromConfig(ebsData.(map[string]interface{}), snapshotMappings[*blockDeviceMapping.DeviceName])
}
}

return blockDeviceMapping
}

func readEbsBlockDeviceFromConfig(ebs map[string]interface{}) *ec2.LaunchTemplateEbsBlockDeviceRequest {
func readEbsBlockDeviceFromConfig(ebs map[string]interface{}, isSnapshot bool) *ec2.LaunchTemplateEbsBlockDeviceRequest {
ebsDevice := &ec2.LaunchTemplateEbsBlockDeviceRequest{}

if v := ebs["delete_on_termination"]; v != nil {
ebsDevice.DeleteOnTermination = aws.Bool(v.(bool))
}

if v := ebs["encrypted"]; v != nil {
ebsDevice.Encrypted = aws.Bool(v.(bool))
if !isSnapshot {
ebsDevice.Encrypted = aws.Bool(v.(bool))
}
}

if v := ebs["iops"].(int); v > 0 {
Expand Down