Skip to content

Commit

Permalink
Merge pull request #9599 from terraform-providers/f-aws_spot_fleet_re…
Browse files Browse the repository at this point in the history
…quest-encryption

resource/aws_spot_fleet_request: Add ebs_block_device and root_block_device kms_key_id argument (support encryption on launch)
  • Loading branch information
bflad authored Aug 7, 2019
2 parents 8291f25 + 12432cb commit e94f7f0
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 0 deletions.
42 changes: 42 additions & 0 deletions aws/resource_aws_spot_fleet_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ func resourceAwsSpotFleetRequest() *schema.Resource {
Computed: true,
ForceNew: true,
},
"kms_key_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"snapshot_id": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -159,12 +165,24 @@ func resourceAwsSpotFleetRequest() *schema.Resource {
Default: true,
ForceNew: true,
},
"encrypted": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
ForceNew: true,
},
"iops": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"kms_key_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"volume_size": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -512,6 +530,10 @@ func readSpotFleetBlockDeviceMappingsFromConfig(
ebs.Encrypted = aws.Bool(v)
}

if v, ok := bd["kms_key_id"].(string); ok && v != "" {
ebs.KmsKeyId = aws.String(v)
}

if v, ok := bd["volume_size"].(int); ok && v != 0 {
ebs.VolumeSize = aws.Int64(int64(v))
}
Expand Down Expand Up @@ -553,6 +575,14 @@ func readSpotFleetBlockDeviceMappingsFromConfig(
DeleteOnTermination: aws.Bool(bd["delete_on_termination"].(bool)),
}

if v, ok := bd["encrypted"].(bool); ok && v {
ebs.Encrypted = aws.Bool(v)
}

if v, ok := bd["kms_key_id"].(string); ok && v != "" {
ebs.KmsKeyId = aws.String(v)
}

if v, ok := bd["volume_size"].(int); ok && v != 0 {
ebs.VolumeSize = aws.Int64(int64(v))
}
Expand Down Expand Up @@ -1065,6 +1095,10 @@ func ebsBlockDevicesToSet(bdm []*ec2.BlockDeviceMapping, rootDevName *string) *s
m["encrypted"] = aws.BoolValue(ebs.Encrypted)
}

if ebs.KmsKeyId != nil {
m["kms_key_id"] = aws.StringValue(ebs.KmsKeyId)
}

if ebs.VolumeSize != nil {
m["volume_size"] = aws.Int64Value(ebs.VolumeSize)
}
Expand Down Expand Up @@ -1117,6 +1151,14 @@ func rootBlockDeviceToSet(
m["delete_on_termination"] = aws.BoolValue(val.Ebs.DeleteOnTermination)
}

if val.Ebs.Encrypted != nil {
m["encrypted"] = aws.BoolValue(val.Ebs.Encrypted)
}

if val.Ebs.KmsKeyId != nil {
m["kms_key_id"] = aws.StringValue(val.Ebs.KmsKeyId)
}

if val.Ebs.VolumeSize != nil {
m["volume_size"] = aws.Int64Value(val.Ebs.VolumeSize)
}
Expand Down
141 changes: 141 additions & 0 deletions aws/resource_aws_spot_fleet_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,48 @@ func TestAccAWSSpotFleetRequest_withEBSDisk(t *testing.T) {
})
}

func TestAccAWSSpotFleetRequest_LaunchSpecification_EbsBlockDevice_KmsKeyId(t *testing.T) {
var config ec2.SpotFleetRequestConfig
rName := acctest.RandString(10)
rInt := acctest.RandInt()
resourceName := "aws_spot_fleet_request.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSEc2SpotFleetRequest(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSpotFleetRequestLaunchSpecificationEbsBlockDeviceKmsKeyId(rName, rInt),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSSpotFleetRequestExists(resourceName, &config),
),
},
},
})
}

func TestAccAWSSpotFleetRequest_LaunchSpecification_RootBlockDevice_KmsKeyId(t *testing.T) {
var config ec2.SpotFleetRequestConfig
rName := acctest.RandString(10)
rInt := acctest.RandInt()
resourceName := "aws_spot_fleet_request.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSEc2SpotFleetRequest(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSpotFleetRequestDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSpotFleetRequestLaunchSpecificationRootBlockDeviceKmsKeyId(rName, rInt),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSSpotFleetRequestExists(resourceName, &config),
),
},
},
})
}

func TestAccAWSSpotFleetRequest_withTags(t *testing.T) {
var config ec2.SpotFleetRequestConfig
rName := acctest.RandString(10)
Expand Down Expand Up @@ -1483,6 +1525,105 @@ resource "aws_spot_fleet_request" "foo" {
`)
}

func testAccAWSSpotFleetRequestLaunchSpecificationEbsBlockDeviceKmsKeyId(rName string, rInt int) string {
return testAccAWSSpotFleetRequestConfigBase(rName, rInt) + fmt.Sprint(`
data "aws_ami" "amzn-ami-minimal-hvm-ebs" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn-ami-minimal-hvm-*"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
}
resource "aws_kms_key" "test" {
deletion_window_in_days = 7
}
resource "aws_spot_fleet_request" "test" {
iam_fleet_role = "${aws_iam_role.test-role.arn}"
spot_price = "0.005"
target_capacity = 1
terminate_instances_with_expiration = true
valid_until = "2029-11-04T20:44:20Z"
wait_for_fulfillment = true
launch_specification {
ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}"
instance_type = "t2.micro"
ebs_block_device {
device_name = "/dev/xvda"
volume_type = "gp2"
volume_size = 8
}
ebs_block_device {
device_name = "/dev/xvdcz"
encrypted = true
kms_key_id = "${aws_kms_key.test.arn}"
volume_type = "gp2"
volume_size = 10
}
}
depends_on = ["aws_iam_policy_attachment.test-attach"]
}
`)
}

func testAccAWSSpotFleetRequestLaunchSpecificationRootBlockDeviceKmsKeyId(rName string, rInt int) string {
return testAccAWSSpotFleetRequestConfigBase(rName, rInt) + fmt.Sprint(`
data "aws_ami" "amzn-ami-minimal-hvm-ebs" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn-ami-minimal-hvm-*"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
}
resource "aws_kms_key" "test" {
deletion_window_in_days = 7
}
resource "aws_spot_fleet_request" "test" {
iam_fleet_role = "${aws_iam_role.test-role.arn}"
spot_price = "0.005"
target_capacity = 1
terminate_instances_with_expiration = true
valid_until = "2029-11-04T20:44:20Z"
wait_for_fulfillment = true
launch_specification {
ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}"
instance_type = "t2.micro"
root_block_device {
encrypted = true
kms_key_id = "${aws_kms_key.test.arn}"
volume_type = "gp2"
volume_size = 10
}
}
depends_on = ["aws_iam_policy_attachment.test-attach"]
}
`)
}

func testAccAWSSpotFleetRequestTagsConfig(rName string, rInt int) string {
return testAccAWSSpotFleetRequestConfigBase(rName, rInt) + fmt.Sprint(`
resource "aws_spot_fleet_request" "foo" {
Expand Down

0 comments on commit e94f7f0

Please sign in to comment.