1. Packages
  2. AWS
  3. API Docs
  4. iam
  5. PolicyAttachment
Viewing docs for AWS v5.43.0 (Older version)
published on Tuesday, Mar 10, 2026 by Pulumi
aws logo
Viewing docs for AWS v5.43.0 (Older version)
published on Tuesday, Mar 10, 2026 by Pulumi

    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 System.Collections.Generic;
    using System.Linq;
    using Pulumi;
    using Aws = Pulumi.Aws;
    
    return await Deployment.RunAsync(() => 
    {
        var user = new Aws.Iam.User("user");
    
        var assumeRole = Aws.Iam.GetPolicyDocument.Invoke(new()
        {
            Statements = new[]
            {
                new Aws.Iam.Inputs.GetPolicyDocumentStatementInputArgs
                {
                    Effect = "Allow",
                    Principals = new[]
                    {
                        new Aws.Iam.Inputs.GetPolicyDocumentStatementPrincipalInputArgs
                        {
                            Type = "Service",
                            Identifiers = new[]
                            {
                                "ec2.amazonaws.com",
                            },
                        },
                    },
                    Actions = new[]
                    {
                        "sts:AssumeRole",
                    },
                },
            },
        });
    
        var role = new Aws.Iam.Role("role", new()
        {
            AssumeRolePolicy = assumeRole.Apply(getPolicyDocumentResult => getPolicyDocumentResult.Json),
        });
    
        var @group = new Aws.Iam.Group("group");
    
        var policyPolicyDocument = Aws.Iam.GetPolicyDocument.Invoke(new()
        {
            Statements = new[]
            {
                new Aws.Iam.Inputs.GetPolicyDocumentStatementInputArgs
                {
                    Effect = "Allow",
                    Actions = new[]
                    {
                        "ec2:Describe*",
                    },
                    Resources = new[]
                    {
                        "*",
                    },
                },
            },
        });
    
        var policyPolicy = new Aws.Iam.Policy("policyPolicy", new()
        {
            Description = "A test policy",
            PolicyDocument = policyPolicyDocument.Apply(getPolicyDocumentResult => getPolicyDocumentResult.Json),
        });
    
        var test_attach = new Aws.Iam.PolicyAttachment("test-attach", new()
        {
            Users = new[]
            {
                user.Name,
            },
            Roles = new[]
            {
                role.Name,
            },
            Groups = new[]
            {
                @group.Name,
            },
            PolicyArn = policyPolicy.Arn,
        });
    
    });
    
    package main
    
    import (
    	"github.com/pulumi/pulumi-aws/sdk/v5/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
    		}
    		assumeRole, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
    			Statements: []iam.GetPolicyDocumentStatement{
    				{
    					Effect: pulumi.StringRef("Allow"),
    					Principals: []iam.GetPolicyDocumentStatementPrincipal{
    						{
    							Type: "Service",
    							Identifiers: []string{
    								"ec2.amazonaws.com",
    							},
    						},
    					},
    					Actions: []string{
    						"sts:AssumeRole",
    					},
    				},
    			},
    		}, nil)
    		if err != nil {
    			return err
    		}
    		role, err := iam.NewRole(ctx, "role", &iam.RoleArgs{
    			AssumeRolePolicy: *pulumi.String(assumeRole.Json),
    		})
    		if err != nil {
    			return err
    		}
    		group, err := iam.NewGroup(ctx, "group", nil)
    		if err != nil {
    			return err
    		}
    		policyPolicyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
    			Statements: []iam.GetPolicyDocumentStatement{
    				{
    					Effect: pulumi.StringRef("Allow"),
    					Actions: []string{
    						"ec2:Describe*",
    					},
    					Resources: []string{
    						"*",
    					},
    				},
    			},
    		}, nil)
    		if err != nil {
    			return err
    		}
    		policyPolicy, err := iam.NewPolicy(ctx, "policyPolicy", &iam.PolicyArgs{
    			Description: pulumi.String("A test policy"),
    			Policy:      *pulumi.String(policyPolicyDocument.Json),
    		})
    		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: policyPolicy.Arn,
    		})
    		if err != nil {
    			return err
    		}
    		return nil
    	})
    }
    
    package generated_program;
    
    import com.pulumi.Context;
    import com.pulumi.Pulumi;
    import com.pulumi.core.Output;
    import com.pulumi.aws.iam.User;
    import com.pulumi.aws.iam.IamFunctions;
    import com.pulumi.aws.iam.inputs.GetPolicyDocumentArgs;
    import com.pulumi.aws.iam.Role;
    import com.pulumi.aws.iam.RoleArgs;
    import com.pulumi.aws.iam.Group;
    import com.pulumi.aws.iam.Policy;
    import com.pulumi.aws.iam.PolicyArgs;
    import com.pulumi.aws.iam.PolicyAttachment;
    import com.pulumi.aws.iam.PolicyAttachmentArgs;
    import java.util.List;
    import java.util.ArrayList;
    import java.util.Map;
    import java.io.File;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    
    public class App {
        public static void main(String[] args) {
            Pulumi.run(App::stack);
        }
    
        public static void stack(Context ctx) {
            var user = new User("user");
    
            final var assumeRole = IamFunctions.getPolicyDocument(GetPolicyDocumentArgs.builder()
                .statements(GetPolicyDocumentStatementArgs.builder()
                    .effect("Allow")
                    .principals(GetPolicyDocumentStatementPrincipalArgs.builder()
                        .type("Service")
                        .identifiers("ec2.amazonaws.com")
                        .build())
                    .actions("sts:AssumeRole")
                    .build())
                .build());
    
            var role = new Role("role", RoleArgs.builder()        
                .assumeRolePolicy(assumeRole.applyValue(getPolicyDocumentResult -> getPolicyDocumentResult.json()))
                .build());
    
            var group = new Group("group");
    
            final var policyPolicyDocument = IamFunctions.getPolicyDocument(GetPolicyDocumentArgs.builder()
                .statements(GetPolicyDocumentStatementArgs.builder()
                    .effect("Allow")
                    .actions("ec2:Describe*")
                    .resources("*")
                    .build())
                .build());
    
            var policyPolicy = new Policy("policyPolicy", PolicyArgs.builder()        
                .description("A test policy")
                .policy(policyPolicyDocument.applyValue(getPolicyDocumentResult -> getPolicyDocumentResult.json()))
                .build());
    
            var test_attach = new PolicyAttachment("test-attach", PolicyAttachmentArgs.builder()        
                .users(user.name())
                .roles(role.name())
                .groups(group.name())
                .policyArn(policyPolicy.arn())
                .build());
    
        }
    }
    
    import * as pulumi from "@pulumi/pulumi";
    import * as aws from "@pulumi/aws";
    
    const user = new aws.iam.User("user", {});
    const assumeRole = aws.iam.getPolicyDocument({
        statements: [{
            effect: "Allow",
            principals: [{
                type: "Service",
                identifiers: ["ec2.amazonaws.com"],
            }],
            actions: ["sts:AssumeRole"],
        }],
    });
    const role = new aws.iam.Role("role", {assumeRolePolicy: assumeRole.then(assumeRole => assumeRole.json)});
    const group = new aws.iam.Group("group", {});
    const policyPolicyDocument = aws.iam.getPolicyDocument({
        statements: [{
            effect: "Allow",
            actions: ["ec2:Describe*"],
            resources: ["*"],
        }],
    });
    const policyPolicy = new aws.iam.Policy("policyPolicy", {
        description: "A test policy",
        policy: policyPolicyDocument.then(policyPolicyDocument => policyPolicyDocument.json),
    });
    const test_attach = new aws.iam.PolicyAttachment("test-attach", {
        users: [user.name],
        roles: [role.name],
        groups: [group.name],
        policyArn: policyPolicy.arn,
    });
    
    import pulumi
    import pulumi_aws as aws
    
    user = aws.iam.User("user")
    assume_role = aws.iam.get_policy_document(statements=[aws.iam.GetPolicyDocumentStatementArgs(
        effect="Allow",
        principals=[aws.iam.GetPolicyDocumentStatementPrincipalArgs(
            type="Service",
            identifiers=["ec2.amazonaws.com"],
        )],
        actions=["sts:AssumeRole"],
    )])
    role = aws.iam.Role("role", assume_role_policy=assume_role.json)
    group = aws.iam.Group("group")
    policy_policy_document = aws.iam.get_policy_document(statements=[aws.iam.GetPolicyDocumentStatementArgs(
        effect="Allow",
        actions=["ec2:Describe*"],
        resources=["*"],
    )])
    policy_policy = aws.iam.Policy("policyPolicy",
        description="A test policy",
        policy=policy_policy_document.json)
    test_attach = aws.iam.PolicyAttachment("test-attach",
        users=[user.name],
        roles=[role.name],
        groups=[group.name],
        policy_arn=policy_policy.arn)
    
    resources:
      user:
        type: aws:iam:User
      role:
        type: aws:iam:Role
        properties:
          assumeRolePolicy: ${assumeRole.json}
      group:
        type: aws:iam:Group
      policyPolicy:
        type: aws:iam:Policy
        properties:
          description: A test policy
          policy: ${policyPolicyDocument.json}
      test-attach:
        type: aws:iam:PolicyAttachment
        properties:
          users:
            - ${user.name}
          roles:
            - ${role.name}
          groups:
            - ${group.name}
          policyArn: ${policyPolicy.arn}
    variables:
      assumeRole:
        fn::invoke:
          Function: aws:iam:getPolicyDocument
          Arguments:
            statements:
              - effect: Allow
                principals:
                  - type: Service
                    identifiers:
                      - ec2.amazonaws.com
                actions:
                  - sts:AssumeRole
      policyPolicyDocument:
        fn::invoke:
          Function: aws:iam:getPolicyDocument
          Arguments:
            statements:
              - effect: Allow
                actions:
                  - ec2:Describe*
                resources:
                  - '*'
    

    Create PolicyAttachment Resource

    Resources are created with functions called constructors. To learn more about declaring and configuring resources, see Resources.

    Constructor syntax

    new PolicyAttachment(name: string, args: PolicyAttachmentArgs, opts?: CustomResourceOptions);
    @overload
    def PolicyAttachment(resource_name: str,
                         args: PolicyAttachmentArgs,
                         opts: Optional[ResourceOptions] = None)
    
    @overload
    def PolicyAttachment(resource_name: str,
                         opts: Optional[ResourceOptions] = None,
                         policy_arn: Optional[str] = None,
                         groups: Optional[Sequence[str]] = None,
                         name: Optional[str] = None,
                         roles: Optional[Sequence[str]] = None,
                         users: Optional[Sequence[str]] = None)
    func NewPolicyAttachment(ctx *Context, name string, args PolicyAttachmentArgs, opts ...ResourceOption) (*PolicyAttachment, error)
    public PolicyAttachment(string name, PolicyAttachmentArgs args, CustomResourceOptions? opts = null)
    public PolicyAttachment(String name, PolicyAttachmentArgs args)
    public PolicyAttachment(String name, PolicyAttachmentArgs args, CustomResourceOptions options)
    
    type: aws:iam:PolicyAttachment
    properties: # The arguments to resource properties.
    options: # Bag of options to control resource's behavior.
    
    

    Parameters

    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.
    name String
    The unique name of the resource.
    args PolicyAttachmentArgs
    The arguments to resource properties.
    options CustomResourceOptions
    Bag of options to control resource's behavior.

    Constructor example

    The following reference example uses placeholder values for all input properties.

    var policyAttachmentResource = new Aws.Iam.PolicyAttachment("policyAttachmentResource", new()
    {
        PolicyArn = "string",
        Groups = new[]
        {
            "string",
        },
        Name = "string",
        Roles = new[]
        {
            "string",
        },
        Users = new[]
        {
            "string",
        },
    });
    
    example, err := iam.NewPolicyAttachment(ctx, "policyAttachmentResource", &iam.PolicyAttachmentArgs{
    	PolicyArn: pulumi.String("string"),
    	Groups: pulumi.Array{
    		pulumi.Any("string"),
    	},
    	Name: pulumi.String("string"),
    	Roles: pulumi.Array{
    		pulumi.Any("string"),
    	},
    	Users: pulumi.Array{
    		pulumi.Any("string"),
    	},
    })
    
    var policyAttachmentResource = new com.pulumi.aws.iam.PolicyAttachment("policyAttachmentResource", com.pulumi.aws.iam.PolicyAttachmentArgs.builder()
        .policyArn("string")
        .groups("string")
        .name("string")
        .roles("string")
        .users("string")
        .build());
    
    policy_attachment_resource = aws.iam.PolicyAttachment("policyAttachmentResource",
        policy_arn="string",
        groups=["string"],
        name="string",
        roles=["string"],
        users=["string"])
    
    const policyAttachmentResource = new aws.iam.PolicyAttachment("policyAttachmentResource", {
        policyArn: "string",
        groups: ["string"],
        name: "string",
        roles: ["string"],
        users: ["string"],
    });
    
    type: aws:iam:PolicyAttachment
    properties:
        groups:
            - string
        name: string
        policyArn: string
        roles:
            - string
        users:
            - string
    

    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

    In Python, inputs that are objects can be passed either as argument classes or as dictionary literals.

    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 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 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
    policyArn
    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

    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 string
    The provider-assigned unique ID for this managed resource.
    id str
    The provider-assigned unique ID for this managed resource.
    id String
    The provider-assigned unique ID for this managed resource.

    Look up 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)
    public static PolicyAttachment get(String name, Output<String> id, PolicyAttachmentState state, CustomResourceOptions options)
    resources:  _:    type: aws:iam:PolicyAttachment    get:      id: ${id}
    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.
    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 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 (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
    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
    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

    Package Details

    Repository
    AWS Classic pulumi/pulumi-aws
    License
    Apache-2.0
    Notes
    This Pulumi package is based on the aws Terraform Provider.
    aws logo
    Viewing docs for AWS v5.43.0 (Older version)
    published on Tuesday, Mar 10, 2026 by Pulumi
      Try Pulumi Cloud free. Your team will thank you.