Skip to content

Commit

Permalink
ecs_service: Role name can be used in iam_role (ARN was supported)
Browse files Browse the repository at this point in the history
 - fixes #2722
  • Loading branch information
radeksimko committed Aug 23, 2015
1 parent fad019e commit 669d196
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 1 deletion.
13 changes: 12 additions & 1 deletion builtin/providers/aws/resource_aws_ecs_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,14 @@ func resourceAwsEcsServiceRead(d *schema.ResourceData, meta interface{}) error {
d.Set("desired_count", *service.DesiredCount)
d.Set("cluster", *service.ClusterArn)

// Save IAM role in the same format
if service.RoleArn != nil {
d.Set("iam_role", *service.RoleArn)
if strings.HasPrefix(d.Get("iam_role").(string), "arn:aws:iam:") {
d.Set("iam_role", *service.RoleArn)
} else {
roleARN := buildIamRoleNameFromARN(*service.RoleArn)
d.Set("iam_role", roleARN)
}
}

if service.LoadBalancers != nil {
Expand Down Expand Up @@ -333,6 +339,11 @@ func buildTaskDefinitionARN(taskDefinition string, meta interface{}) (string, er
return arn, nil
}

func buildIamRoleNameFromARN(arn string) string {
// arn:aws:iam::0123456789:role/EcsService
return strings.Split(arn, "/")[1]
}

func parseTaskDefinition(taskDefinition string) (string, string, error) {
matches := taskDefinitionRE.FindAllStringSubmatch(taskDefinition, 2)

Expand Down
109 changes: 109 additions & 0 deletions builtin/providers/aws/resource_aws_ecs_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,22 @@ func TestAccAWSEcsServiceWithRenamedCluster(t *testing.T) {
})
}

func TestAccAWSEcsService_withIamRole(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEcsServiceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSEcsService_withIamRole,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEcsServiceExists("aws_ecs_service.ghost"),
),
},
},
})
}

func testAccCheckAWSEcsServiceDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ecsconn

Expand Down Expand Up @@ -253,6 +269,99 @@ resource "aws_ecs_service" "mongo" {
}
`

var testAccAWSEcsService_withIamRole = `
resource "aws_ecs_cluster" "main" {
name = "terraformecstest11"
}
resource "aws_ecs_task_definition" "ghost" {
family = "ghost_service"
container_definitions = <<DEFINITION
[
{
"cpu": 128,
"essential": true,
"image": "ghost:latest",
"memory": 128,
"name": "ghost",
"portMappings": [
{
"containerPort": 2368,
"hostPort": 8080
}
]
}
]
DEFINITION
}
resource "aws_iam_role" "ecs_service" {
name = "EcsService"
assume_role_policy = <<EOF
{
"Version": "2008-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {"AWS": "*"},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy" "ecs_service" {
name = "EcsService"
role = "${aws_iam_role.ecs_service.name}"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"elasticloadbalancing:*",
"ec2:*",
"ecs:*"
],
"Resource": [
"*"
]
}
]
}
EOF
}
resource "aws_elb" "main" {
name = "foobar-terraform-test"
availability_zones = ["us-west-2a"]
listener {
instance_port = 8080
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
}
resource "aws_ecs_service" "ghost" {
name = "ghost"
cluster = "${aws_ecs_cluster.main.id}"
task_definition = "${aws_ecs_task_definition.ghost.arn}"
desired_count = 1
iam_role = "${aws_iam_role.ecs_service.name}"
load_balancer {
elb_name = "${aws_elb.main.id}"
container_name = "ghost"
container_port = "2368"
}
}
`

var testAccAWSEcsServiceWithFamilyAndRevision = `
resource "aws_ecs_cluster" "default" {
name = "terraformecstest2"
Expand Down

0 comments on commit 669d196

Please sign in to comment.