PolicyAttachment class
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 managedPolicyArns 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.
> NOTE: To ensure Pulumi correctly manages dependencies during updates, use a reference to the IAM resource when defining the policyArn for aws.iam.PolicyAttachment, rather than constructing the ARN directly. For example, use policyArn = aws_iam_policy.example.arn instead of policyArn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:policy/Example". Failing to do so may lead to errors like DeleteConflict: Cannot delete a policy attached to entities or NoSuchEntity.
Example Usage
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
const user = new aws.iam.User("user", {name: "example-user"});
const assumeRole = aws.iam.getPolicyDocument({
statements: [{
principals: [{
type: "Service",
identifiers: ["ec2.amazonaws.com"],
}],
effect: "Allow",
actions: ["sts:AssumeRole"],
}],
});
const role = new aws.iam.Role("role", {
name: "example-role",
assumeRolePolicy: assumeRole.then(assumeRole => assumeRole.json),
});
const group = new aws.iam.Group("group", {name: "example-group"});
const policy = aws.iam.getPolicyDocument({
statements: [{
effect: "Allow",
actions: ["ec2:Describe*"],
resources: ["*"],
}],
});
const policyPolicy = new aws.iam.Policy("policy", {
name: "example-policy",
description: "An example policy",
policy: policy.then(policy => policy.json),
});
const example_attach = new aws.iam.PolicyAttachment("example-attach", {
name: "example-attachment",
users: [user.name],
roles: [role.name],
groups: [group.name],
policyArn: policyPolicy.arn,
});
import pulumi
import pulumi_aws as aws
user = aws.iam.User("user", name="example-user")
assume_role = aws.iam.get_policy_document(statements=[{
"principals": [{
"type": "Service",
"identifiers": ["ec2.amazonaws.com"],
}],
"effect": "Allow",
"actions": ["sts:AssumeRole"],
}])
role = aws.iam.Role("role",
name="example-role",
assume_role_policy=assume_role.json)
group = aws.iam.Group("group", name="example-group")
policy = aws.iam.get_policy_document(statements=[{
"effect": "Allow",
"actions": ["ec2:Describe*"],
"resources": ["*"],
}])
policy_policy = aws.iam.Policy("policy",
name="example-policy",
description="An example policy",
policy=policy.json)
example_attach = aws.iam.PolicyAttachment("example-attach",
name="example-attachment",
users=[user.name],
roles=[role.name],
groups=[group.name],
policy_arn=policy_policy.arn)
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Aws = Pulumi.Aws;
return await Deployment.RunAsync(() =>
{
var user = new Aws.Iam.User("user", new()
{
Name = "example-user",
});
var assumeRole = Aws.Iam.GetPolicyDocument.Invoke(new()
{
Statements = new[]
{
new Aws.Iam.Inputs.GetPolicyDocumentStatementInputArgs
{
Principals = new[]
{
new Aws.Iam.Inputs.GetPolicyDocumentStatementPrincipalInputArgs
{
Type = "Service",
Identifiers = new[]
{
"ec2.amazonaws.com",
},
},
},
Effect = "Allow",
Actions = new[]
{
"sts:AssumeRole",
},
},
},
});
var role = new Aws.Iam.Role("role", new()
{
Name = "example-role",
AssumeRolePolicy = assumeRole.Apply(getPolicyDocumentResult => getPolicyDocumentResult.Json),
});
var @group = new Aws.Iam.Group("group", new()
{
Name = "example-group",
});
var policy = 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("policy", new()
{
Name = "example-policy",
Description = "An example policy",
PolicyDocument = policy.Apply(getPolicyDocumentResult => getPolicyDocumentResult.Json),
});
var example_attach = new Aws.Iam.PolicyAttachment("example-attach", new()
{
Name = "example-attachment",
Users =
{
user.Name,
},
Roles =
{
role.Name,
},
Groups =
{
@group.Name,
},
PolicyArn = policyPolicy.Arn,
});
});
package main
import (
"github.com/pulumi/pulumi-aws/sdk/v7/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", &iam.UserArgs{
Name: pulumi.String("example-user"),
})
if err != nil {
return err
}
assumeRole, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
Statements: []iam.GetPolicyDocumentStatement{
{
Principals: []iam.GetPolicyDocumentStatementPrincipal{
{
Type: "Service",
Identifiers: []string{
"ec2.amazonaws.com",
},
},
},
Effect: pulumi.StringRef("Allow"),
Actions: []string{
"sts:AssumeRole",
},
},
},
}, nil)
if err != nil {
return err
}
role, err := iam.NewRole(ctx, "role", &iam.RoleArgs{
Name: pulumi.String("example-role"),
AssumeRolePolicy: pulumi.String(assumeRole.Json),
})
if err != nil {
return err
}
group, err := iam.NewGroup(ctx, "group", &iam.GroupArgs{
Name: pulumi.String("example-group"),
})
if err != nil {
return err
}
policy, 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, "policy", &iam.PolicyArgs{
Name: pulumi.String("example-policy"),
Description: pulumi.String("An example policy"),
Policy: pulumi.String(policy.Json),
})
if err != nil {
return err
}
_, err = iam.NewPolicyAttachment(ctx, "example-attach", &iam.PolicyAttachmentArgs{
Name: pulumi.String("example-attachment"),
Users: pulumi.Array{
user.Name,
},
Roles: pulumi.Array{
role.Name,
},
Groups: pulumi.Array{
group.Name,
},
PolicyArn: policyPolicy.Arn,
})
if err != nil {
return err
}
return nil
})
}
pulumi {
required_providers {
aws = {
source = "pulumi/aws"
}
}
}
data "aws_iam_getpolicydocument" "assumeRole" {
statements {
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
effect = "Allow"
actions = ["sts:AssumeRole"]
}
}
data "aws_iam_getpolicydocument" "policy" {
statements {
effect = "Allow"
actions = ["ec2:Describe*"]
resources = ["*"]
}
}
resource "aws_iam_user" "user" {
name = "example-user"
}
resource "aws_iam_role" "role" {
name = "example-role"
assume_role_policy = data.aws_iam_getpolicydocument.assumeRole.json
}
resource "aws_iam_group" "group" {
name = "example-group"
}
resource "aws_iam_policy" "policy" {
name = "example-policy"
description = "An example policy"
policy = data.aws_iam_getpolicydocument.policy.json
}
resource "aws_iam_policyattachment" "example-attach" {
name = "example-attachment"
users = [aws_iam_user.user.name]
roles = [aws_iam_role.role.name]
groups = [aws_iam_group.group.name]
policy_arn = aws_iam_policy.policy.arn
}
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.UserArgs;
import com.pulumi.aws.iam.IamFunctions;
import com.pulumi.aws.iam.inputs.GetPolicyDocumentArgs;
import com.pulumi.aws.iam.inputs.GetPolicyDocumentStatementArgs;
import com.pulumi.aws.iam.inputs.GetPolicyDocumentStatementPrincipalArgs;
import com.pulumi.aws.iam.Role;
import com.pulumi.aws.iam.RoleArgs;
import com.pulumi.aws.iam.Group;
import com.pulumi.aws.iam.GroupArgs;
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.ArrayList;
import java.util.Arrays;
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", UserArgs.builder()
.name("example-user")
.build());
final var assumeRole = IamFunctions.getPolicyDocument(GetPolicyDocumentArgs.builder()
.statements(GetPolicyDocumentStatementArgs.builder()
.principals(GetPolicyDocumentStatementPrincipalArgs.builder()
.type("Service")
.identifiers("ec2.amazonaws.com")
.build())
.effect("Allow")
.actions("sts:AssumeRole")
.build())
.build());
var role = new Role("role", RoleArgs.builder()
.name("example-role")
.assumeRolePolicy(assumeRole.json())
.build());
var group = new Group("group", GroupArgs.builder()
.name("example-group")
.build());
final var policy = IamFunctions.getPolicyDocument(GetPolicyDocumentArgs.builder()
.statements(GetPolicyDocumentStatementArgs.builder()
.effect("Allow")
.actions("ec2:Describe*")
.resources("*")
.build())
.build());
var policyPolicy = new Policy("policyPolicy", PolicyArgs.builder()
.name("example-policy")
.description("An example policy")
.policy(policy.json())
.build());
var example_attach = new PolicyAttachment("example-attach", PolicyAttachmentArgs.builder()
.name("example-attachment")
.users(user.name())
.roles(role.name())
.groups(group.name())
.policyArn(policyPolicy.arn())
.build());
}
}
resources:
user:
type: aws:iam:User
properties:
name: example-user
role:
type: aws:iam:Role
properties:
name: example-role
assumeRolePolicy: ${assumeRole.json}
group:
type: aws:iam:Group
properties:
name: example-group
policyPolicy:
type: aws:iam:Policy
name: policy
properties:
name: example-policy
description: An example policy
policy: ${policy.json}
example-attach:
type: aws:iam:PolicyAttachment
properties:
name: example-attachment
users:
- ${user.name}
roles:
- ${role.name}
groups:
- ${group.name}
policyArn: ${policyPolicy.arn}
variables:
assumeRole:
fn::invoke:
function: aws:iam:getPolicyDocument
arguments:
statements:
- principals:
- type: Service
identifiers:
- ec2.amazonaws.com
effect: Allow
actions:
- sts:AssumeRole
policy:
fn::invoke:
function: aws:iam:getPolicyDocument
arguments:
statements:
- effect: Allow
actions:
- ec2:Describe*
resources:
- '*'
Constructors
- PolicyAttachment(String name, {PolicyAttachmentArgs? args, CustomResourceOptions? options})
-
Creates a new PolicyAttachment.
nameThe Pulumi resource name.argsArguments used to configure this PolicyAttachment. The set of arguments for PolicyAttachment.optionsResource options controlling this resource's behavior. - PolicyAttachment.reference(String urn)
- Creates a typed reference to an existing PolicyAttachment resource.
Properties
-
childResources
→ Set<
Resource> -
finalinherited
-
completionSources
↔ Map<
String, IOutputCompletionSource> -
latefinalinherited
-
groups
↔ Output<
List< String> ?> -
Group(s) the policy should be applied to.
latefinal
- hashCode → int
-
The hash code for this object.
no setterinherited
-
id
↔ Output<
String> -
getter/setter pairinherited
- isCustom → bool
-
Returns whether this resource is provider-managed.
no setterinherited
- isProtected → bool
-
Returns whether this resource is protected from deletion.
no setterinherited
- isRemote → bool
-
Whether this resource is registered as remote.
no setterinherited
- isResourceReference → bool
-
Whether this instance represents a resource value returned over RPC.
finalinherited
-
name
↔ Output<
String> -
Name of the attachment. This cannot be an empty string.
latefinal
-
policyArn
↔ Output<
String> -
ARN of the policy you want to apply. Typically this should be a reference to the ARN of another resource to ensure dependency ordering, such as
aws_iam_policy.example.arn.latefinal -
resourceTransforms
→ List<
ResourceTransform> -
Inherited/explicit async transforms.
no setterinherited
-
roles
↔ Output<
List< String> ?> -
Role(s) the policy should be applied to.
latefinal
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
transformations
→ List<
ResourceTransformation> -
Inherited/explicit legacy transformations.
no setterinherited
-
urn
↔ Output<
String> -
latefinalinherited
-
users
↔ Output<
List< String> ?> -
User(s) the policy should be applied to.
latefinal
Methods
-
failId(
Object error) → void -
Completes this resource ID with an error when registration fails.
inherited
-
failOutputs(
Object error) → void -
Completes all output properties with
error.inherited -
failUrn(
Object error) → void -
Completes this resource URN with an error when registration fails.
inherited
-
getProvider(
String moduleMember) → ProviderResource? -
Returns provider for
moduleMember's package, if configured.inherited -
getResourceName(
) → String -
Returns this resource's logical name.
inherited
-
getResourceType(
) → String -
Returns this resource's Pulumi type token.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
registerOutput<
T> (String propertyName, {Object? decoder(Object?)?, bool isSecret = false}) → Output< T> -
Registers a dynamic output property for this resource.
inherited
-
resolveId(
String? value, {required bool isKnown}) → void -
Resolves the provider-assigned ID for this resource.
inherited
-
resolveOutputs(
Struct outputs) → void -
Resolves all output properties from a monitor response payload.
inherited
-
resolveUrn(
String value) → void -
Resolves this resource's URN once assigned by the engine.
inherited
-
serializeProperties(
Map< String, dynamic> properties) → Future<Struct> -
Serializes resource properties for RPC transmission.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
get(
String name, Input< String> id, {PolicyAttachmentState? state, CustomResourceOptions? options}) → PolicyAttachment -
Gets an existing PolicyAttachment resource's state with the given
nameandid.