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

Add outpost_arn to instance datasource #12330

Merged
merged 9 commits into from
Apr 29, 2020
5 changes: 5 additions & 0 deletions aws/data_source_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ func dataSourceAwsInstance() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"outpost_arn": {
Type: schema.TypeString,
Computed: true,
},
"network_interface_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -421,6 +425,7 @@ func instanceDescriptionAttributes(d *schema.ResourceData, instance *ec2.Instanc
d.Set("public_ip", instance.PublicIpAddress)
d.Set("private_dns", instance.PrivateDnsName)
d.Set("private_ip", instance.PrivateIpAddress)
d.Set("outpost_arn", instance.OutpostArn)
d.Set("iam_instance_profile", iamInstanceProfileArnToName(instance.IamInstanceProfile))

// iterate through network interfaces, and set subnet, network_interface, public_addr
Expand Down
3 changes: 2 additions & 1 deletion aws/data_source_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ func TestAccAWSInstanceDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrPair(datasourceName, "ami", resourceName, "ami"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"),
resource.TestCheckResourceAttrPair(datasourceName, "instance_type", resourceName, "instance_type"),
resource.TestMatchResourceAttr(datasourceName, "arn", regexp.MustCompile(`^arn:[^:]+:ec2:[^:]+:\d{12}:instance/i-.+`)),
testAccMatchResourceAttrRegionalARN(datasourceName, "arn", "ec2", regexp.MustCompile(`instance/i-.+`)),
resource.TestCheckNoResourceAttr(datasourceName, "user_data_base64"),
resource.TestCheckResourceAttr(datasourceName, "outpost_arn", ""),
),
},
},
Expand Down
6 changes: 6 additions & 0 deletions aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ func resourceAwsInstance() *schema.Resource {
Computed: true,
},

"outpost_arn": {
Type: schema.TypeString,
Computed: true,
},

"network_interface_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -761,6 +766,7 @@ func resourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error {
d.Set("public_ip", instance.PublicIpAddress)
d.Set("private_dns", instance.PrivateDnsName)
d.Set("private_ip", instance.PrivateIpAddress)
d.Set("outpost_arn", instance.OutpostArn)
d.Set("iam_instance_profile", iamInstanceProfileArnToName(instance.IamInstanceProfile))

// Set configured Network Interface Device Index Slice
Expand Down
73 changes: 73 additions & 0 deletions aws/resource_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ func TestAccAWSInstance_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "user_data", "3dc39dda39be1205215e776bad998da361a5955d"),
resource.TestCheckResourceAttr(resourceName, "root_block_device.#", "0"), // This is an instance store AMI
resource.TestCheckResourceAttr(resourceName, "ebs_block_device.#", "0"),
resource.TestCheckResourceAttr(resourceName, "outpost_arn", ""),
resource.TestMatchResourceAttr(resourceName, "arn", regexp.MustCompile(`^arn:[^:]+:ec2:[^:]+:\d{12}:instance/i-.+`)),
),
},
Expand Down Expand Up @@ -813,6 +814,43 @@ func TestAccAWSInstance_vpc(t *testing.T) {
})
}

func TestAccAWSInstance_outpost(t *testing.T) {
var v ec2.Instance
resourceName := "aws_instance.test"

outpostArn := os.Getenv("AWS_OUTPOST_ARN")
if outpostArn == "" {
t.Skip(
"Environment variable AWS_OUTPOST_ARN is not set. " +
"This environment variable must be set to the ARN of " +
"a deployed Outpost to enable this test.")
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: resourceName,
IDRefreshIgnore: []string{"associate_public_ip_address"},
Providers: testAccProviders,
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccInstanceConfigOutpost(outpostArn),
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceExists(
resourceName, &v),
resource.TestCheckResourceAttr(
resourceName, "outpost_arn", outpostArn),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSInstance_placementGroup(t *testing.T) {
var v ec2.Instance
resourceName := "aws_instance.test"
Expand Down Expand Up @@ -3362,6 +3400,41 @@ resource "aws_instance" "test" {
`)
}

func testAccInstanceConfigOutpost(outpostArn string) string {
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceOutpostConfig(outpostArn) + fmt.Sprintf(`
resource "aws_instance" "test" {
ami = "${data.aws_ami.amzn-ami-minimal-hvm-ebs.id}"
instance_type = "m5.large"
subnet_id = "${aws_subnet.test.id}"

root_block_device {
volume_type = "gp2"
volume_size = 8
}
}
`)
}

func testAccAwsInstanceOutpostConfig(outpostArn string) string {
return fmt.Sprintf(`
data "aws_availability_zones" "current" {
# Exclude usw2-az4 (us-west-2d) as it has limited instance types.
blacklisted_zone_ids = ["usw2-az4"]
}

resource "aws_vpc" "test" {
cidr_block = "10.1.0.0/16"
}

resource "aws_subnet" "test" {
cidr_block = "10.1.1.0/24"
vpc_id = "${aws_vpc.test.id}"
availability_zone = "${data.aws_availability_zones.current.names[0]}"
outpost_arn = "%s"
}
`, outpostArn)
}

func testAccInstanceConfigPlacementGroup(rName string) string {
return testAccLatestAmazonLinuxHvmEbsAmiConfig() + testAccAwsInstanceVpcConfig(rName, false) + fmt.Sprintf(`
resource "aws_placement_group" "test" {
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ interpolation.
* `security_groups` - The associated security groups.
* `source_dest_check` - Whether the network interface performs source/destination checking (Boolean).
* `subnet_id` - The VPC subnet ID.
* `outpost_arn` - The Amazon Resource Name (ARN) of the Outpost.
* `user_data` - SHA-1 hash of User Data supplied to the Instance.
* `user_data_base64` - Base64 encoded contents of User Data supplied to the Instance. Valid UTF-8 contents can be decoded with the [`base64decode` function](/docs/configuration/functions/base64decode.html). This attribute is only exported if `get_user_data` is true.
* `tags` - A map of tags assigned to the Instance.
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ In addition to all arguments above, the following attributes are exported:
* `security_groups` - The associated security groups.
* `vpc_security_group_ids` - The associated security groups in non-default VPC
* `subnet_id` - The VPC subnet ID.
* `outpost_arn` - The ARN of the Outpost the instance is assigned to.
* `credit_specification` - Credit specification of instance.
* `instance_state` - The state of the instance. One of: `pending`, `running`, `shutting-down`, `terminated`, `stopping`, `stopped`. See [Instance Lifecycle](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-lifecycle.html) for more information.

Expand Down