Skip to content

Commit fa8d7f0

Browse files
builtinnyastack72
authored andcommitted
provider/aws: add aws_ecs_task_definition datasource (hashicorp#8509)
1 parent c61bc1f commit fa8d7f0

5 files changed

Lines changed: 221 additions & 0 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package aws
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/aws/aws-sdk-go/aws"
7+
"github.com/aws/aws-sdk-go/service/ecs"
8+
"github.com/hashicorp/terraform/helper/schema"
9+
)
10+
11+
func dataSourceAwsEcsTaskDefinition() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceAwsEcsTaskDefinitionRead,
14+
15+
Schema: map[string]*schema.Schema{
16+
"task_definition": &schema.Schema{
17+
Type: schema.TypeString,
18+
Required: true,
19+
ForceNew: true,
20+
},
21+
// Computed values.
22+
"family": &schema.Schema{
23+
Type: schema.TypeString,
24+
Computed: true,
25+
},
26+
"network_mode": &schema.Schema{
27+
Type: schema.TypeString,
28+
Computed: true,
29+
},
30+
"revision": &schema.Schema{
31+
Type: schema.TypeInt,
32+
Computed: true,
33+
},
34+
"status": &schema.Schema{
35+
Type: schema.TypeString,
36+
Computed: true,
37+
},
38+
"task_role_arn": &schema.Schema{
39+
Type: schema.TypeString,
40+
Computed: true,
41+
},
42+
},
43+
}
44+
}
45+
46+
func dataSourceAwsEcsTaskDefinitionRead(d *schema.ResourceData, meta interface{}) error {
47+
conn := meta.(*AWSClient).ecsconn
48+
49+
desc, err := conn.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{
50+
TaskDefinition: aws.String(d.Get("task_definition").(string)),
51+
})
52+
53+
if err != nil {
54+
return err
55+
}
56+
57+
taskDefinition := *desc.TaskDefinition
58+
59+
d.SetId(aws.StringValue(taskDefinition.TaskDefinitionArn))
60+
d.Set("family", aws.StringValue(taskDefinition.Family))
61+
d.Set("network_mode", aws.StringValue(taskDefinition.NetworkMode))
62+
d.Set("revision", aws.Int64Value(taskDefinition.Revision))
63+
d.Set("status", aws.StringValue(taskDefinition.Status))
64+
d.Set("task_role_arn", aws.StringValue(taskDefinition.TaskRoleArn))
65+
66+
if d.Id() == "" {
67+
return fmt.Errorf("task definition %q not found", d.Get("task_definition").(string))
68+
}
69+
70+
return nil
71+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package aws
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/resource"
8+
)
9+
10+
func TestAccAWSEcsDataSource_ecsTaskDefinition(t *testing.T) {
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() { testAccPreCheck(t) },
13+
Providers: testAccProviders,
14+
Steps: []resource.TestStep{
15+
resource.TestStep{
16+
Config: testAccCheckAwsEcsTaskDefinitionDataSourceConfig,
17+
Check: resource.ComposeTestCheckFunc(
18+
resource.TestMatchResourceAttr("data.aws_ecs_task_definition.mongo", "id", regexp.MustCompile("^arn:aws:ecs:us-west-2:[0-9]{12}:task-definition/mongodb:[1-9]*[0-9]$")),
19+
resource.TestCheckResourceAttr("data.aws_ecs_task_definition.mongo", "family", "mongodb"),
20+
resource.TestCheckResourceAttr("data.aws_ecs_task_definition.mongo", "network_mode", "bridge"),
21+
resource.TestMatchResourceAttr("data.aws_ecs_task_definition.mongo", "revision", regexp.MustCompile("^[1-9]*[0-9]$")),
22+
resource.TestCheckResourceAttr("data.aws_ecs_task_definition.mongo", "status", "ACTIVE"),
23+
resource.TestMatchResourceAttr("data.aws_ecs_task_definition.mongo", "task_role_arn", regexp.MustCompile("^arn:aws:iam::[0-9]{12}:role/mongo_role$")),
24+
),
25+
},
26+
},
27+
})
28+
}
29+
30+
const testAccCheckAwsEcsTaskDefinitionDataSourceConfig = `
31+
resource "aws_iam_role" "mongo_role" {
32+
name = "mongo_role"
33+
assume_role_policy = <<POLICY
34+
{
35+
"Version": "2012-10-17",
36+
"Statement": [
37+
{
38+
"Action": "sts:AssumeRole",
39+
"Principal": {
40+
"Service": "ec2.amazonaws.com"
41+
},
42+
"Effect": "Allow",
43+
"Sid": ""
44+
}
45+
]
46+
}
47+
POLICY
48+
}
49+
50+
resource "aws_ecs_task_definition" "mongo" {
51+
family = "mongodb"
52+
task_role_arn = "${aws_iam_role.mongo_role.arn}"
53+
network_mode = "bridge"
54+
container_definitions = <<DEFINITION
55+
[
56+
{
57+
"cpu": 128,
58+
"environment": [{
59+
"name": "SECRET",
60+
"value": "KEY"
61+
}],
62+
"essential": true,
63+
"image": "mongo:latest",
64+
"memory": 128,
65+
"memoryReservation": 64,
66+
"name": "mongodb"
67+
}
68+
]
69+
DEFINITION
70+
}
71+
72+
data "aws_ecs_task_definition" "mongo" {
73+
task_definition = "${aws_ecs_task_definition.mongo.family}"
74+
}
75+
`

builtin/providers/aws/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ func Provider() terraform.ResourceProvider {
164164
"aws_ebs_snapshot": dataSourceAwsEbsSnapshot(),
165165
"aws_ebs_volume": dataSourceAwsEbsVolume(),
166166
"aws_ecs_container_definition": dataSourceAwsEcsContainerDefinition(),
167+
"aws_ecs_task_definition": dataSourceAwsEcsTaskDefinition(),
167168
"aws_eip": dataSourceAwsEip(),
168169
"aws_elb_hosted_zone_id": dataSourceAwsElbHostedZoneId(),
169170
"aws_elb_service_account": dataSourceAwsElbServiceAccount(),
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
layout: "aws"
3+
page_title: "AWS: aws_ecs_task_definition"
4+
sidebar_current: "docs-aws-datasource-ecs-task-definition"
5+
description: |-
6+
Provides details about an ecs task definition
7+
---
8+
9+
# aws\_ecs\_task\_definition
10+
11+
The ECS task definition data source allows access to details of
12+
a specific AWS ECS task definition.
13+
14+
15+
## Example Usage
16+
17+
```
18+
# Simply specify the family to find the latest ACTIVE revision in that family.
19+
data "aws_ecs_task_definition" "mongo" {
20+
task_definition = "${aws_ecs_task_definition.mongo.family}"
21+
}
22+
23+
resource "aws_ecs_cluster" "foo" {
24+
name = "foo"
25+
}
26+
27+
resource "aws_ecs_task_definition" "mongo" {
28+
family = "mongodb"
29+
container_definitions = <<DEFINITION
30+
[
31+
{
32+
"cpu": 128,
33+
"environment": [{
34+
"name": "SECRET",
35+
"value": "KEY"
36+
}],
37+
"essential": true,
38+
"image": "mongo:latest",
39+
"memory": 128,
40+
"memoryReservation": 64,
41+
"name": "mongodb"
42+
}
43+
]
44+
DEFINITION
45+
}
46+
47+
resource "aws_ecs_service" "mongo" {
48+
name = "mongo"
49+
cluster = "${aws_ecs_cluster.foo.id}"
50+
desired_count = 2
51+
52+
# Track the latest ACTIVE revision
53+
task_definition = "${aws_ecs_task_definition.mongo.family}:${max("${aws_ecs_task_definition.mongo.revision}", "${data.aws_ecs_task_definition.mongo.revision}")}"
54+
}
55+
```
56+
57+
## Argument Reference
58+
59+
The following arguments are supported:
60+
61+
* `task_definition` - (Required) The family for the latest ACTIVE revision, family and revision (family:revision) for a specific revision in the family, the ARN of the task definition to access to.
62+
63+
## Attributes Reference
64+
65+
The following attributes are exported:
66+
67+
* `family` - The family of this task definition
68+
* `network_mode` - The Docker networking mode to use for the containers in this task.
69+
* `revision` - The revision of this task definition
70+
* `status` - The status of this task definition
71+
* `task_role_arn` - The ARN of the IAM role that containers in this task can assume

website/source/layouts/aws.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
<li<%= sidebar_current("docs-aws-datasource-ecs-container-definition") %>>
5454
<a href="/docs/providers/aws/d/ecs_container_definition.html">aws_ecs_container_definition</a>
5555
</li>
56+
<li<%= sidebar_current("docs-aws-datasource-ecs-task-definition") %>>
57+
<a href="/docs/providers/aws/d/ecs_task_definition.html">aws_ecs_task_definition</a>
58+
</li>
5659
<li<%= sidebar_current("docs-aws-datasource-elb-hosted-zone-id") %>>
5760
<a href="/docs/providers/aws/d/elb_hosted_zone_id.html">aws_elb_hosted_zone_id</a>
5861
</li>

0 commit comments

Comments
 (0)