AWS IAM Policy

This page shows how to write Terraform and CloudFormation for IAM Policy and write them securely.

aws_iam_policy (Terraform)

The Policy in IAM can be configured in Terraform with the resource name aws_iam_policy. The following sections describe 5 examples of how to use the resource and its parameters.

Example Usage from GitHub

iam_policy.tf#L1
resource "aws_iam_policy" "AWSLambdaBasicExecutionRole-3f87559a-531c-4789-b832-2b77f0d84004" {
  name = "AWSLambdaBasicExecutionRole-3f87559a-531c-4789-b832-2b77f0d84004"
  path = "/service-role/"

  policy = <<POLICY
{
iam_policy.tf#L1
resource "aws_iam_policy" "AWSLambdaBasicExecutionRole-3f87559a-531c-4789-b832-2b77f0d84004" {
  name = "AWSLambdaBasicExecutionRole-3f87559a-531c-4789-b832-2b77f0d84004"
  path = "/service-role/"

  policy = <<POLICY
{
security_audit_extras_policy.tf#L27
resource "aws_iam_policy" "audit_securityauditextras_policy" {
  provider = aws.audit

  description = var.securityauditextras_policy_description
  name        = var.securityauditextras_policy_name
  policy      = data.aws_iam_policy_document.securityauditextras_doc.json
iam_policies.tf#L19
resource "aws_iam_policy" "aggregate_metrics_update" {
  name   = "CovidAlertAggregateMetricsUpdateItem"
  path   = "/"
  policy = data.aws_iam_policy_document.aggregate_metrics_update.json
}

policies.tf#L3
resource "aws_iam_policy" "enforce_mfa_policy" {
  path        = "/"
  description = "block users from acccessing anything unless they are mfa auth'd"
  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [

Review your Terraform file for AWS best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

Explanation in Terraform Registry

Provides an IAM policy.

Tips: Best Practices for The Other AWS IAM Resources

In addition to the aws_iam_account_password_policy, AWS IAM has the other resources that should be configured for security reasons. Please check some examples of those resources and precautions.

risk-label

aws_iam_account_password_policy

Ensure AWS IAM account password policies requires long passwords

It's better to enforce the use of long and complex passwords to reduce the risk of bruteforce attacks.

Review your AWS IAM settings

In addition to the above, there are other security points you should be aware of making sure that your .tf files are protected in Shisho Cloud.

AWS::IAM::Policy (CloudFormation)

The Policy in IAM can be configured in CloudFormation with the resource name AWS::IAM::Policy. The following sections describe 10 examples of how to use the resource and its parameters.

Example Usage from GitHub

FAILED.yml#L5
    Type: 'AWS::IAM::Policy'
    Properties:
      PolicyName: root
      PolicyDocument:
        Version: 2012-10-17
        Statement:
FAILED.yml#L5
    Type: 'AWS::IAM::Policy'
    Properties:
      PolicyName: root
      PolicyDocument:
        Version: 2012-10-17
        Statement:
skillbrowser-lambda.yml#L49
    Type: AWS::IAM::Policy
    Properties:
      PolicyName: LambdaFunctionPolicy
      Roles:
      - !Ref 'LambdaRole'
      PolicyDocument:
roles.yml#L30
    Type: "AWS::IAM::Policy"
    DependsOn: SSODashboardRole
    Properties:
      PolicyDocument:
        Version: "2012-10-17"
        Statement:
serverless.yml#L123
      Type: AWS::IAM::Policy
      Properties:
        PolicyName: serverless-event-logging
        PolicyDocument:
          Version: "2012-10-17"
          Statement:
s3-cloudformation-template.json#L310
            "Type": "AWS::IAM::Policy",
            "Properties": {
                "PolicyName": {
                    "Ref": "s3PublicPolicy"
                },
                "Roles": [
s3-cloudformation-template.json#L259
          "Type": "AWS::IAM::Policy",
          "Properties": {
            "PolicyName": "superadmin-group-s3-policy",
            "Roles": [
                {
                    "Fn::Join": [
s3-cloudformation-template.json#L240
            "Type": "AWS::IAM::Policy",
            "Properties": {
                "PolicyName": {
                    "Ref": "s3PublicPolicy"
                },
                "Roles": [
s3-cloudformation-template.json#L240
            "Type": "AWS::IAM::Policy",
            "Properties": {
                "PolicyName": {
                    "Ref": "s3PublicPolicy"
                },
                "Roles": [
s3-cloudformation-template.json#L214
            "Type": "AWS::IAM::Policy",
            "Properties": {
                "PolicyName": {
                    "Ref": "s3PublicPolicy"
                },
                "Roles": [

Parameters

Explanation in CloudFormation Registry

Adds or updates an inline policy document that is embedded in the specified IAM user, group, or role.

An IAM user can also have a managed policy attached to it. For information about policies, see Managed Policies and Inline Policies in the IAM User Guide.

The Groups, Roles, and Users properties are optional. However, you must specify at least one of these properties.

For information about limits on the number of inline policies that you can embed in an identity, see Limitations on IAM Entities in the IAM User Guide.

Frequently asked questions

What is AWS IAM Policy?

AWS IAM Policy is a resource for IAM of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.

Where can I find the example code for the AWS IAM Policy?

For Terraform, the mortyre/misc, mortyre/misc and cisagov/cool-auditor-iam source code examples are useful. See the Terraform Example section for further details.

For CloudFormation, the melscoop-test/check, melscoop-test/check and victorsalaun/skillbrowser-aws-serverless source code examples are useful. See the CloudFormation Example section for further details.