Merge #4276: aws_opsworks_instance resource

This commit is contained in:
Martin Atkins 2016-04-16 10:14:22 -07:00
commit 94f338d5de
5 changed files with 1428 additions and 0 deletions

View File

@ -213,6 +213,7 @@ func Provider() terraform.ResourceProvider {
"aws_opsworks_mysql_layer": resourceAwsOpsworksMysqlLayer(), "aws_opsworks_mysql_layer": resourceAwsOpsworksMysqlLayer(),
"aws_opsworks_ganglia_layer": resourceAwsOpsworksGangliaLayer(), "aws_opsworks_ganglia_layer": resourceAwsOpsworksGangliaLayer(),
"aws_opsworks_custom_layer": resourceAwsOpsworksCustomLayer(), "aws_opsworks_custom_layer": resourceAwsOpsworksCustomLayer(),
"aws_opsworks_instance": resourceAwsOpsworksInstance(),
"aws_placement_group": resourceAwsPlacementGroup(), "aws_placement_group": resourceAwsPlacementGroup(),
"aws_proxy_protocol_policy": resourceAwsProxyProtocolPolicy(), "aws_proxy_protocol_policy": resourceAwsProxyProtocolPolicy(),
"aws_rds_cluster": resourceAwsRDSCluster(), "aws_rds_cluster": resourceAwsRDSCluster(),

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,274 @@
package aws
import (
"fmt"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/opsworks"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccAWSOpsworksInstance(t *testing.T) {
stackName := fmt.Sprintf("tf-%d", acctest.RandInt())
var opsinst opsworks.Instance
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsOpsworksInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAwsOpsworksInstanceConfigCreate(stackName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSOpsworksInstanceExists(
"aws_opsworks_instance.tf-acc", &opsinst),
testAccCheckAWSOpsworksInstanceAttributes(&opsinst),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "hostname", "tf-acc1",
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "instance_type", "t2.micro",
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "state", "stopped",
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "layer_ids.#", "1",
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "install_updates_on_boot", "true",
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "architecture", "x86_64",
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "os", "Amazon Linux 2014.09", // inherited from opsworks_stack_test
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "root_device_type", "ebs", // inherited from opsworks_stack_test
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "availability_zone", "us-west-2a", // inherited from opsworks_stack_test
),
),
},
resource.TestStep{
Config: testAccAwsOpsworksInstanceConfigUpdate(stackName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSOpsworksInstanceExists(
"aws_opsworks_instance.tf-acc", &opsinst),
testAccCheckAWSOpsworksInstanceAttributes(&opsinst),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "hostname", "tf-acc1",
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "instance_type", "t2.small",
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "layer_ids.#", "2",
),
resource.TestCheckResourceAttr(
"aws_opsworks_instance.tf-acc", "os", "Amazon Linux 2015.09",
),
),
},
},
})
}
func testAccCheckAWSOpsworksInstanceExists(
n string, opsinst *opsworks.Instance) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No Opsworks Instance is set")
}
conn := testAccProvider.Meta().(*AWSClient).opsworksconn
params := &opsworks.DescribeInstancesInput{
InstanceIds: []*string{&rs.Primary.ID},
}
resp, err := conn.DescribeInstances(params)
if err != nil {
return err
}
if v := len(resp.Instances); v != 1 {
return fmt.Errorf("Expected 1 request returned, got %d", v)
}
*opsinst = *resp.Instances[0]
return nil
}
}
func testAccCheckAWSOpsworksInstanceAttributes(
opsinst *opsworks.Instance) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Depending on the timing, the state could be requested or stopped
if *opsinst.Status != "stopped" && *opsinst.Status != "requested" {
return fmt.Errorf("Unexpected request status: %s", *opsinst.Status)
}
if *opsinst.AvailabilityZone != "us-west-2a" {
return fmt.Errorf("Unexpected availability zone: %s", *opsinst.AvailabilityZone)
}
if *opsinst.Architecture != "x86_64" {
return fmt.Errorf("Unexpected architecture: %s", *opsinst.Architecture)
}
if *opsinst.InfrastructureClass != "ec2" {
return fmt.Errorf("Unexpected infrastructure class: %s", *opsinst.InfrastructureClass)
}
if *opsinst.RootDeviceType != "ebs" {
return fmt.Errorf("Unexpected root device type: %s", *opsinst.RootDeviceType)
}
if *opsinst.VirtualizationType != "hvm" {
return fmt.Errorf("Unexpected virtualization type: %s", *opsinst.VirtualizationType)
}
return nil
}
}
func testAccCheckAwsOpsworksInstanceDestroy(s *terraform.State) error {
opsworksconn := testAccProvider.Meta().(*AWSClient).opsworksconn
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_opsworks_instance" {
continue
}
req := &opsworks.DescribeInstancesInput{
InstanceIds: []*string{
aws.String(rs.Primary.ID),
},
}
_, err := opsworksconn.DescribeInstances(req)
if err != nil {
if awserr, ok := err.(awserr.Error); ok {
if awserr.Code() == "ResourceNotFoundException" {
// not found, good to go
return nil
}
}
return err
}
}
return fmt.Errorf("Fall through error on OpsWorks instance test")
}
func testAccAwsOpsworksInstanceConfigCreate(name string) string {
return fmt.Sprintf(`
resource "aws_security_group" "tf-ops-acc-web" {
name = "%s-web"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "tf-ops-acc-php" {
name = "%s-php"
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_opsworks_static_web_layer" "tf-acc" {
stack_id = "${aws_opsworks_stack.tf-acc.id}"
custom_security_group_ids = [
"${aws_security_group.tf-ops-acc-web.id}",
]
}
resource "aws_opsworks_php_app_layer" "tf-acc" {
stack_id = "${aws_opsworks_stack.tf-acc.id}"
custom_security_group_ids = [
"${aws_security_group.tf-ops-acc-php.id}",
]
}
resource "aws_opsworks_instance" "tf-acc" {
stack_id = "${aws_opsworks_stack.tf-acc.id}"
layer_ids = [
"${aws_opsworks_static_web_layer.tf-acc.id}",
]
instance_type = "t2.micro"
state = "stopped"
hostname = "tf-acc1"
}
%s
`, name, name, testAccAwsOpsworksStackConfigVpcCreate(name))
}
func testAccAwsOpsworksInstanceConfigUpdate(name string) string {
return fmt.Sprintf(`
resource "aws_security_group" "tf-ops-acc-web" {
name = "%s-web"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "tf-ops-acc-php" {
name = "%s-php"
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_opsworks_static_web_layer" "tf-acc" {
stack_id = "${aws_opsworks_stack.tf-acc.id}"
custom_security_group_ids = [
"${aws_security_group.tf-ops-acc-web.id}",
]
}
resource "aws_opsworks_php_app_layer" "tf-acc" {
stack_id = "${aws_opsworks_stack.tf-acc.id}"
custom_security_group_ids = [
"${aws_security_group.tf-ops-acc-php.id}",
]
}
resource "aws_opsworks_instance" "tf-acc" {
stack_id = "${aws_opsworks_stack.tf-acc.id}"
layer_ids = [
"${aws_opsworks_static_web_layer.tf-acc.id}",
"${aws_opsworks_php_app_layer.tf-acc.id}",
]
instance_type = "t2.small"
state = "stopped"
hostname = "tf-acc1"
os = "Amazon Linux 2015.09"
}
%s
`, name, name, testAccAwsOpsworksStackConfigVpcCreate(name))
}

View File

@ -0,0 +1,136 @@
---
layout: "aws"
page_title: "AWS: aws_opsworks_instance"
sidebar_current: "docs-aws-resource-opsworks-instance"
description: |-
Provides an OpsWorks instance resource.
---
# aws\_opsworks\_instance
Provides an OpsWorks instance resource.
## Example Usage
```
aws_opsworks_instance" "my-instance" {
stack_id = "${aws_opsworks_stack.my-stack.id}"
layer_ids = [
"${aws_opsworks_custom_layer.my-layer.id}",
]
instance_type = "t2.micro"
os = "Amazon Linux 2015.09"
state = "stopped"
}
```
## Argument Reference
The following arguments are supported:
* `instance_type` - (Required) The type of instance to start
* `stack_id` - (Required) The id of the stack the instance will belong to.
* `layer_ids` - (Required) The ids of the layers the instance will belong to.
* `state` - (Optional) The desired state of the instance. Can be either `"running"` or `"stopped"`.
* `install_updates_on_boot` - (Optional) Controls where to install OS and package updates when the instance boots. Defaults to `true`.
* `auto_scaling_type` - (Optional) Creates load-based or time-based instances. If set, can be either: `"load"` or `"timer"`.
* `availability_zone` - (Optional) Name of the availability zone where instances will be created
by default.
* `ebs_optimized` - (Optional) If true, the launched EC2 instance will be EBS-optimized.
* `hostname` - (Optional) The instance's host name.
* `architecture` - (Optional) Machine architecture for created instances. Can be either `"x86_64"` (the default) or `"i386"`
* `ami_id` - (Optional) The AMI to use for the instance. If an AMI is specified, `os` must be `"Custom"`.
* `os` - (Optional) Name of operating system that will be installed.
* `root_device_type` - (Optional) Name of the type of root device instances will have by default. Can be either `"ebs"` or `"instance-store"`
* `ssh_key_name` - (Optional) Name of the SSH keypair that instances will have by default.
* `agent_version` - (Optional) The AWS OpsWorks agent to install. Defaults to `"INHERIT"`.
* `subnet_id` - (Optional) Subnet ID to attach to
* `virtualization_type` - (Optional) Keyword to choose what virtualization mode created instances
will use. Can be either `"paravirtual"` or `"hvm"`.
* `root_block_device` - (Optional) Customize details about the root block
device of the instance. See [Block Devices](#block-devices) below for details.
* `ebs_block_device` - (Optional) Additional EBS block devices to attach to the
instance. See [Block Devices](#block-devices) below for details.
* `ephemeral_block_device` - (Optional) Customize Ephemeral (also known as
"Instance Store") volumes on the instance. See [Block Devices](#block-devices) below for details.
<a id="block-devices"></a>
## Block devices
Each of the `*_block_device` attributes controls a portion of the AWS
Instance's "Block Device Mapping". It's a good idea to familiarize yourself with [AWS's Block Device
Mapping docs](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/block-device-mapping-concepts.html)
to understand the implications of using these attributes.
The `root_block_device` mapping supports the following:
* `volume_type` - (Optional) The type of volume. Can be `"standard"`, `"gp2"`,
or `"io1"`. (Default: `"standard"`).
* `volume_size` - (Optional) The size of the volume in gigabytes.
* `iops` - (Optional) The amount of provisioned
[IOPS](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-io-characteristics.html).
This must be set with a `volume_type` of `"io1"`.
* `delete_on_termination` - (Optional) Whether the volume should be destroyed
on instance termination (Default: `true`).
Modifying any of the `root_block_device` settings requires resource
replacement.
Each `ebs_block_device` supports the following:
* `device_name` - The name of the device to mount.
* `snapshot_id` - (Optional) The Snapshot ID to mount.
* `volume_type` - (Optional) The type of volume. Can be `"standard"`, `"gp2"`,
or `"io1"`. (Default: `"standard"`).
* `volume_size` - (Optional) The size of the volume in gigabytes.
* `iops` - (Optional) The amount of provisioned
[IOPS](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-io-characteristics.html).
This must be set with a `volume_type` of `"io1"`.
* `delete_on_termination` - (Optional) Whether the volume should be destroyed
on instance termination (Default: `true`).
* `encrypted` - (Optional) Enables [EBS
encryption](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html)
on the volume (Default: `false`). Cannot be used with `snapshot_id`.
Modifying any `ebs_block_device` currently requires resource replacement.
Each `ephemeral_block_device` supports the following:
* `device_name` - The name of the block device to mount on the instance.
* `virtual_name` - The [Instance Store Device
Name](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/InstanceStorage.html#InstanceStoreDeviceNames)
(e.g. `"ephemeral0"`)
Each AWS Instance type has a different set of Instance Store block devices
available for attachment. AWS [publishes a
list](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/InstanceStorage.html#StorageOnInstanceTypes)
of which ephemeral devices are available on each type. The devices are always
identified by the `virtual_name` in the format `"ephemeral{0..N}"`.
~> **NOTE:** Currently, changes to `*_block_device` configuration of _existing_
resources cannot be automatically detected by Terraform. After making updates
to block device configuration, resource recreation can be manually triggered by
using the [`taint` command](/docs/commands/taint.html).
## Attributes Reference
The following attributes are exported:
* `id` - The id of the OpsWorks instance.
* `agent_version` - The AWS OpsWorks agent version.
* `availability_zone` - The availability zone of the instance.
* `ssh_key_name` - The key name of the instance
* `public_dns` - The public DNS name assigned to the instance. For EC2-VPC, this
is only available if you've enabled DNS hostnames for your VPC
* `public_ip` - The public IP address assigned to the instance, if applicable.
* `private_dns` - The private DNS name assigned to the instance. Can only be
used inside the Amazon EC2, and only available if you've enabled DNS hostnames
for your VPC
* `private_ip` - The private IP address assigned to the instance
* `subnet_id` - The VPC subnet ID.
* `security_group_ids` - The associated security groups.

View File

@ -478,6 +478,10 @@
<a href="/docs/providers/aws/r/opsworks_haproxy_layer.html">aws_opsworks_haproxy_layer</a> <a href="/docs/providers/aws/r/opsworks_haproxy_layer.html">aws_opsworks_haproxy_layer</a>
</li> </li>
<li<%= sidebar_current("docs-aws-resource-opsworks-instance") %>>
<a href="/docs/providers/aws/r/opsworks_instance.html">aws_opsworks_instance</a>
</li>
<li<%= sidebar_current("docs-aws-resource-opsworks-java-app-layer") %>> <li<%= sidebar_current("docs-aws-resource-opsworks-java-app-layer") %>>
<a href="/docs/providers/aws/r/opsworks_java_app_layer.html">aws_opsworks_java_app_layer</a> <a href="/docs/providers/aws/r/opsworks_java_app_layer.html">aws_opsworks_java_app_layer</a>
</li> </li>