Skip to main content

IAM Roles With Policies

Easily define IAM roles and policies with minimal configuration.

Features

  • Simplifies the creation of IAM roles and policies on AWS with minimal configuration
  • Uses the high-level IamRoles (IamRole) composite component for easy IAM definition
  • Supports defining assume_role_service to auto-generate the assume role policy document
  • Load naming match policy from config/iam-role-policies folder
  • Supports attaching named policies

Source Code

import { AwsStack } from '@dinghy/tf-aws'
import { IamRoles } from '@dinghy/tf-aws/iam'

export default () => (
<AwsStack>
<IamRoles />
</AwsStack>
)

Policies

Policies are resolved from the config/iam-role-policies folder. For each IAM role, policies are loaded in this priority order, based on the role's name:

  1. my-first-role
    1. first.yml
    2. my.yml
    3. default.yml
  2. my-second-role
    1. my-second-role.yml
    2. my.yml
    3. default.yml
# https://github.com/dinghydev/dinghy/blob/main/core/tf-aws/src/composites/iam/types.ts#RolePolicySchema
id-override-demo-policy:
- id: override-by-id
permission: readonly
buckets:
- dinghy-demo-site-overridable
eu-west-2-activated-services-all-access:
- resources:
- arn:aws:ec2:eu-west-2:*:*
- arn:aws:ecs:eu-west-2:*:*

Outputs

Resource types

List of resource types used by this stack, in approximate order of creation:

  1. aws_iam_role
  2. aws_iam_role_policy_attachment
  3. aws_iam_role_policy

Steps to try

Install dinghy-cli

If you haven't already:

curl -fsSL https://get.dinghy.dev/install.sh | sh

Sample Screenshot

Prepare source code

Create my-roles.tsx and dinghy.config.yml files with content from above.

curl -fsSL --create-dirs -o iam-roles/my-roles.tsx https://raw.githubusercontent.com/dinghydev/dinghy/main/sites/www/src/docs/examples/show-cases/iam-roles/my-roles.tsx
curl -fsSL --create-dirs -o iam-roles/dinghy.config.yml https://raw.githubusercontent.com/dinghydev/dinghy/main/sites/www/src/docs/examples/show-cases/iam-roles/dinghy.config.yml
curl -fsSL --create-dirs -o iam-roles/config/iam-role-policies/default.yml https://raw.githubusercontent.com/dinghydev/dinghy/main/sites/www/src/docs/examples/show-cases/iam-roles/config/iam-role-policies/default.yml
curl -fsSL --create-dirs -o iam-roles/config/iam-role-policies/first.yml https://raw.githubusercontent.com/dinghydev/dinghy/main/sites/www/src/docs/examples/show-cases/iam-roles/config/iam-role-policies/first.yml
curl -fsSL --create-dirs -o iam-roles/config/iam-role-policies/my-second-role.yml https://raw.githubusercontent.com/dinghydev/dinghy/main/sites/www/src/docs/examples/show-cases/iam-roles/config/iam-role-policies/my-second-role.yml
curl -fsSL --create-dirs -o iam-roles/config/iam-role-policies/my.yml https://raw.githubusercontent.com/dinghydev/dinghy/main/sites/www/src/docs/examples/show-cases/iam-roles/config/iam-role-policies/my.yml
cd iam-roles

Preview the actions

Run dinghy tf diff to preview the Terraform actions that will be performed.

info

Make sure your AWS credentials are configured before interacting with AWS services.

dinghy tf diff

Sample Screenshot

Apply the actions

Run dinghy tf deploy to apply the changes and provision your resources.

dinghy tf deploy

Sample Screenshot

Created Roles

info

The inline policy details are clearly organized by service.

my-first-role

my-second-role

Destroy resources

After experimenting, run dinghy tf destroy to remove all resources created in the previous steps.