AWS Classic

v4.30.0 published on Tuesday, Nov 30, 2021 by Pulumi

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.

NOTE: For a given role, this resource is incompatible with using the aws.iam.Role resource managed_policy_arns argument. When using that argument and this resource, both will attempt to manage the role’s managed policy attachments and the provider will show a permanent difference.

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
        {
            Users = 
            {
                user.Name,
            },
            Roles = 
            {
                role.Name,
            },
            Groups = 
            {
                @group.Name,
            },
            PolicyArn = policy.Arn,
        });
    }

}
package main

import (
	"fmt"

	"github.com/pulumi/pulumi-aws/sdk/v4/go/aws"
	"github.com/pulumi/pulumi-aws/sdk/v4/go/aws/iam"
	"github.com/pulumi/pulumi/sdk/v3/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.Any(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\": \"sts:AssumeRole\",\n", "      \"Principal\": {\n", "        \"Service\": \"ec2.amazonaws.com\"\n", "      },\n", "      \"Effect\": \"Allow\",\n", "      \"Sid\": \"\"\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.Any(fmt.Sprintf("%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")),
		})
		if err != nil {
			return err
		}
		_, err = iam.NewPolicyAttachment(ctx, "test_attach", &iam.PolicyAttachmentArgs{
			Users: pulumi.AnyArray{
				user.Name,
			},
			Roles: pulumi.AnyArray{
				role.Name,
			},
			Groups: pulumi.AnyArray{
				group.Name,
			},
			PolicyArn: policy.Arn,
		})
		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",
    users=[user.name],
    roles=[role.name],
    groups=[group.name],
    policy_arn=policy.arn)
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", {
    users: [user.name],
    roles: [role.name],
    groups: [group.name],
    policyArn: policy.arn,
});

Create a PolicyAttachment Resource

new PolicyAttachment(name: string, args: PolicyAttachmentArgs, opts?: CustomResourceOptions);
@overload
def PolicyAttachment(resource_name: str,
                     opts: Optional[ResourceOptions] = None,
                     groups: Optional[Sequence[str]] = None,
                     name: Optional[str] = None,
                     policy_arn: Optional[str] = None,
                     roles: Optional[Sequence[str]] = None,
                     users: Optional[Sequence[str]] = None)
@overload
def PolicyAttachment(resource_name: str,
                     args: PolicyAttachmentArgs,
                     opts: Optional[ResourceOptions] = None)
func NewPolicyAttachment(ctx *Context, name string, args PolicyAttachmentArgs, opts ...ResourceOption) (*PolicyAttachment, error)
public PolicyAttachment(string name, PolicyAttachmentArgs args, CustomResourceOptions? opts = null)
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.
args PolicyAttachmentArgs
The arguments to resource properties.
opts ResourceOptions
Bag of options to control 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 Architecture and Concepts 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 Sequence[str]
The group(s) the policy should be applied to
name str
The name of the attachment. This cannot be an empty string.
roles Sequence[str]
The role(s) the policy should be applied to
users Sequence[str]
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
@staticmethod
def get(resource_name: str,
        id: str,
        opts: Optional[ResourceOptions] = None,
        groups: Optional[Sequence[str]] = None,
        name: Optional[str] = None,
        policy_arn: Optional[str] = None,
        roles: Optional[Sequence[str]] = None,
        users: Optional[Sequence[str]] = None) -> PolicyAttachment
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 Sequence[str]
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 Sequence[str]
The role(s) the policy should be applied to
users Sequence[str]
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.