Skip to content

PolicyAttachment

The PolicyAttachment resource lets you attach AWS IAM policies to IAM roles.

Minimal Example

Attach an AWS managed policy to a role:

ts
import { PolicyAttachment } from "alchemy/aws";

const adminAccess = await PolicyAttachment("admin-policy", {
  policyArn: "arn:aws:iam::aws:policy/AdministratorAccess", 
  roleName: role.name
});

Attach Custom Policy

Attach a custom policy created with the Policy resource:

ts
import { PolicyAttachment, Policy } from "alchemy/aws";

const customPolicy = await Policy("custom-policy", {
  policyName: "custom-policy",
  document: {
    Version: "2012-10-17",
    Statement: [{
      Effect: "Allow",
      Action: ["s3:ListBucket"],
      Resource: "*"
    }]
  }
});

const attachment = await PolicyAttachment("custom-policy", {
  policyArn: customPolicy.arn,
  roleName: role.name
});

Multiple Policy Attachments

Attach multiple policies to a role:

ts
import { PolicyAttachment } from "alchemy/aws";

const s3Access = await PolicyAttachment("s3-access", {
  policyArn: "arn:aws:iam::aws:policy/AmazonS3FullAccess",
  roleName: role.name
});

const sqsAccess = await PolicyAttachment("sqs-access", {
  policyArn: "arn:aws:iam::aws:policy/AmazonSQSFullAccess", 
  roleName: role.name
});