PolicyAttachment

Attaches a Managed IAM Policy to user(s), role(s), and/or group(s)

!> WARNING: The aws.iam.PolicyAttachment resource creates exclusive attachments of IAM policies. Across the entire AWS account, all of the users/roles/groups to which a single policy is attached must be declared by a single aws.iam.PolicyAttachment resource. This means that even any users/roles/groups that have the attached policy via any other mechanism (including other resources managed by this provider) will have that attached policy revoked by this resource. Consider aws.iam.RolePolicyAttachment, aws.iam.UserPolicyAttachment, or aws.iam.GroupPolicyAttachment instead. These resources do not enforce exclusive attachment of an IAM policy.

NOTE: The usage of this resource conflicts with the aws.iam.GroupPolicyAttachment, aws.iam.RolePolicyAttachment, and aws.iam.UserPolicyAttachment resources and will permanently show a difference if both are defined.

Example Usage

using Pulumi;
using Aws = Pulumi.Aws;

class MyStack : Stack
{
    public MyStack()
    {
        var user = new Aws.Iam.User("user", new Aws.Iam.UserArgs
        {
        });
        var role = new Aws.Iam.Role("role", new Aws.Iam.RoleArgs
        {
            AssumeRolePolicy = @"{
  ""Version"": ""2012-10-17"",
  ""Statement"": [
    {
      ""Action"": ""sts:AssumeRole"",
      ""Principal"": {
        ""Service"": ""ec2.amazonaws.com""
      },
      ""Effect"": ""Allow"",
      ""Sid"": """"
    }
  ]
}

",
        });
        var @group = new Aws.Iam.Group("group", new Aws.Iam.GroupArgs
        {
        });
        var policy = new Aws.Iam.Policy("policy", new Aws.Iam.PolicyArgs
        {
            Description = "A test policy",
            Policy = @"{
  ""Version"": ""2012-10-17"",
  ""Statement"": [
    {
      ""Action"": [
        ""ec2:Describe*""
      ],
      ""Effect"": ""Allow"",
      ""Resource"": ""*""
    }
  ]
}

",
        });
        var test_attach = new Aws.Iam.PolicyAttachment("test-attach", new Aws.Iam.PolicyAttachmentArgs
        {
            Groups = 
            {
                @group.Name,
            },
            PolicyArn = policy.Arn,
            Roles = 
            {
                role.Name,
            },
            Users = 
            {
                user.Name,
            },
        });
    }

}
package main

import (
    "fmt"

    "github.com/pulumi/pulumi-aws/sdk/v2/go/aws/iam"
    "github.com/pulumi/pulumi/sdk/v2/go/pulumi"
)

func main() {
    pulumi.Run(func(ctx *pulumi.Context) error {
        user, err := iam.NewUser(ctx, "user", nil)
        if err != nil {
            return err
        }
        role, err := iam.NewRole(ctx, "role", &iam.RoleArgs{
            AssumeRolePolicy: pulumi.String(fmt.Sprintf("%v%v%v%v%v%v%v%v%v%v%v%v%v%v", "{\n", "  \"Version\": \"2012-10-17\",\n", "  \"Statement\": [\n", "    {\n", "      \"Action\": \"sts:AssumeRole\",\n", "      \"Principal\": {\n", "        \"Service\": \"ec2.amazonaws.com\"\n", "      },\n", "      \"Effect\": \"Allow\",\n", "      \"Sid\": \"\"\n", "    }\n", "  ]\n", "}\n", "\n")),
        })
        if err != nil {
            return err
        }
        group, err := iam.NewGroup(ctx, "group", nil)
        if err != nil {
            return err
        }
        policy, err := iam.NewPolicy(ctx, "policy", &iam.PolicyArgs{
            Description: pulumi.String("A test policy"),
            Policy:      pulumi.String(fmt.Sprintf("%v%v%v%v%v%v%v%v%v%v%v%v%v", "{\n", "  \"Version\": \"2012-10-17\",\n", "  \"Statement\": [\n", "    {\n", "      \"Action\": [\n", "        \"ec2:Describe*\"\n", "      ],\n", "      \"Effect\": \"Allow\",\n", "      \"Resource\": \"*\"\n", "    }\n", "  ]\n", "}\n", "\n")),
        })
        if err != nil {
            return err
        }
        _, err = iam.NewPolicyAttachment(ctx, "test_attach", &iam.PolicyAttachmentArgs{
            Groups: pulumi.StringArray{
                group.Name,
            },
            PolicyArn: policy.Arn,
            Roles: pulumi.StringArray{
                role.Name,
            },
            Users: pulumi.StringArray{
                user.Name,
            },
        })
        if err != nil {
            return err
        }
        return nil
    })
}
import pulumi
import pulumi_aws as aws

user = aws.iam.User("user")
role = aws.iam.Role("role", assume_role_policy="""{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": "sts:AssumeRole",
      "Principal": {
        "Service": "ec2.amazonaws.com"
      },
      "Effect": "Allow",
      "Sid": ""
    }
  ]
}

""")
group = aws.iam.Group("group")
policy = aws.iam.Policy("policy",
    description="A test policy",
    policy="""{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "ec2:Describe*"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}

""")
test_attach = aws.iam.PolicyAttachment("test-attach",
    groups=[group.name],
    policy_arn=policy.arn,
    roles=[role.name],
    users=[user.name])
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const user = new aws.iam.User("user", {});
const role = new aws.iam.Role("role", {
    assumeRolePolicy: `{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": "sts:AssumeRole",
      "Principal": {
        "Service": "ec2.amazonaws.com"
      },
      "Effect": "Allow",
      "Sid": ""
    }
  ]
}
`,
});
const group = new aws.iam.Group("group", {});
const policy = new aws.iam.Policy("policy", {
    description: "A test policy",
    policy: `{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "ec2:Describe*"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}
`,
});
const test_attach = new aws.iam.PolicyAttachment("test-attach", {
    groups: [group.name],
    policyArn: policy.arn,
    roles: [role.name],
    users: [user.name],
});

Create a PolicyAttachment Resource

def PolicyAttachment(resource_name, opts=None, groups=None, name=None, policy_arn=None, roles=None, users=None, __props__=None);
name string
The unique name of the resource.
args PolicyAttachmentArgs
The arguments to resource properties.
opts CustomResourceOptions
Bag of options to control resource's behavior.
resource_name str
The unique name of the resource.
opts ResourceOptions
A bag of options that control this resource's behavior.
ctx Context
Context object for the current deployment.
name string
The unique name of the resource.
args PolicyAttachmentArgs
The arguments to resource properties.
opts ResourceOption
Bag of options to control resource's behavior.
name string
The unique name of the resource.
args PolicyAttachmentArgs
The arguments to resource properties.
opts CustomResourceOptions
Bag of options to control resource's behavior.

PolicyAttachment Resource Properties

To learn more about resource properties and how to use them, see Inputs and Outputs in the Programming Model docs.

Inputs

The PolicyAttachment resource accepts the following input properties:

PolicyArn string

The ARN of the policy you want to apply

Groups List<string>

The group(s) the policy should be applied to

Name string

The name of the attachment. This cannot be an empty string.

Roles List<string>

The role(s) the policy should be applied to

Users List<string>

The user(s) the policy should be applied to

PolicyArn string

The ARN of the policy you want to apply

Groups []interface{}

The group(s) the policy should be applied to

Name string

The name of the attachment. This cannot be an empty string.

Roles []interface{}

The role(s) the policy should be applied to

Users []interface{}

The user(s) the policy should be applied to

policyArn ARN

The ARN of the policy you want to apply

groups string | Group[]

The group(s) the policy should be applied to

name string

The name of the attachment. This cannot be an empty string.

roles string | Role[]

The role(s) the policy should be applied to

users string | User[]

The user(s) the policy should be applied to

policy_arn str

The ARN of the policy you want to apply

groups List[Group, Default=String>]

The group(s) the policy should be applied to

name str

The name of the attachment. This cannot be an empty string.

roles List[Role, Default=String>]

The role(s) the policy should be applied to

users List[User, Default=String>]

The user(s) the policy should be applied to

Outputs

All input properties are implicitly available as output properties. Additionally, the PolicyAttachment resource produces the following output properties:

Id string
The provider-assigned unique ID for this managed resource.
Id string
The provider-assigned unique ID for this managed resource.
id string
The provider-assigned unique ID for this managed resource.
id str
The provider-assigned unique ID for this managed resource.

Look up an Existing PolicyAttachment Resource

Get an existing PolicyAttachment resource’s state with the given name, ID, and optional extra properties used to qualify the lookup.

public static get(name: string, id: Input<ID>, state?: PolicyAttachmentState, opts?: CustomResourceOptions): PolicyAttachment
static get(resource_name, id, opts=None, groups=None, name=None, policy_arn=None, roles=None, users=None, __props__=None);
func GetPolicyAttachment(ctx *Context, name string, id IDInput, state *PolicyAttachmentState, opts ...ResourceOption) (*PolicyAttachment, error)
public static PolicyAttachment Get(string name, Input<string> id, PolicyAttachmentState? state, CustomResourceOptions? opts = null)
name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.
resource_name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.
name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.

The following state arguments are supported:

Groups List<string>

The group(s) the policy should be applied to

Name string

The name of the attachment. This cannot be an empty string.

PolicyArn string

The ARN of the policy you want to apply

Roles List<string>

The role(s) the policy should be applied to

Users List<string>

The user(s) the policy should be applied to

Groups []interface{}

The group(s) the policy should be applied to

Name string

The name of the attachment. This cannot be an empty string.

PolicyArn string

The ARN of the policy you want to apply

Roles []interface{}

The role(s) the policy should be applied to

Users []interface{}

The user(s) the policy should be applied to

groups string | Group[]

The group(s) the policy should be applied to

name string

The name of the attachment. This cannot be an empty string.

policyArn ARN

The ARN of the policy you want to apply

roles string | Role[]

The role(s) the policy should be applied to

users string | User[]

The user(s) the policy should be applied to

groups List[Group, Default=String>]

The group(s) the policy should be applied to

name str

The name of the attachment. This cannot be an empty string.

policy_arn str

The ARN of the policy you want to apply

roles List[Role, Default=String>]

The role(s) the policy should be applied to

users List[User, Default=String>]

The user(s) the policy should be applied to

Package Details

Repository
https://github.com/pulumi/pulumi-aws
License
Apache-2.0
Notes
This Pulumi package is based on the aws Terraform Provider.