<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alessandro Gaggia</title>
    <description>The latest articles on DEV Community by Alessandro Gaggia (@urz9999).</description>
    <link>https://dev.to/urz9999</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F494928%2Feeb19ebc-6e8f-4c06-a1a1-14ca7374972d.jpeg</url>
      <title>DEV Community: Alessandro Gaggia</title>
      <link>https://dev.to/urz9999</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/urz9999"/>
    <language>en</language>
    <item>
      <title>Increase AWS Security Posture with IAM Roles and Policies</title>
      <dc:creator>Alessandro Gaggia</dc:creator>
      <pubDate>Thu, 03 Aug 2023 12:00:00 +0000</pubDate>
      <link>https://dev.to/aws-builders/increase-aws-security-posture-with-iam-roles-and-policies-44d6</link>
      <guid>https://dev.to/aws-builders/increase-aws-security-posture-with-iam-roles-and-policies-44d6</guid>
      <description>&lt;p&gt;In this article we will discuss identity and access management (IAM) within Amazon Web Services (AWS), focusing on the similarities and differences between IAM roles and policies. &lt;/p&gt;

&lt;p&gt;This article will provide examples of both roles and policies and explains how they relate to IAM roles, users, and groups. &lt;/p&gt;

&lt;p&gt;By the end, you will have a clear understanding of IAM within AWS and be better equipped to manage access and identity needs on the platform.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an IAM Policy in AWS?
&lt;/h2&gt;

&lt;p&gt;An IAM policy is a document that &lt;strong&gt;defines permissions for AWS resources&lt;/strong&gt;. It is an essential component of AWS identity and access management (IAM) that provides fine-grained control over who can access specific resources in your AWS account. &lt;/p&gt;

&lt;p&gt;IAM policies &lt;strong&gt;are used to grant or deny access to AWS resources&lt;/strong&gt; such as Amazon S3 buckets, EC2 instances, and RDS databases.&lt;/p&gt;

&lt;p&gt;IAM policies are written in JSON format and consist of one or more statements. Each statement contains a set of permissions for a specific resource or set of resources. For example, you might use an IAM policy to grant read-only access to a particular S3 bucket, or to allow a user to start and stop an EC2 instance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Elements of an IAM Policy
&lt;/h2&gt;

&lt;p&gt;An IAM policy is comprised of the following main elements:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Element&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Version&lt;/td&gt;
&lt;td&gt;The version of the IAM policy language used.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Statement&lt;/td&gt;
&lt;td&gt;A list of one or more statements that define the permissions for the policy.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Effect&lt;/td&gt;
&lt;td&gt;Specifies whether the statement allows or denies access to the resource.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Action&lt;/td&gt;
&lt;td&gt;The specific actions that are allowed or denied for the resource.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Resource&lt;/td&gt;
&lt;td&gt;The AWS resource that the policy applies to.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Principal&lt;/td&gt;
&lt;td&gt;The entity that the policy applies to.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Version
&lt;/h3&gt;

&lt;p&gt;The Version element specifies the version of the IAM policy language used in the policy.&lt;/p&gt;

&lt;p&gt;The current version of the policy language is October 17, 2012.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "Version": "2012-10-17",
    ...
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Statement
&lt;/h3&gt;

&lt;p&gt;The Statement element is a list of one or more statements that define the permissions for the policy.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "Version": "2012-10-17",
    "Statement": [
        {
            ...
        },
        {
            ...
        }
    ]
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Effect
&lt;/h3&gt;

&lt;p&gt;The Effect element specifies whether the statement allows or denies access to the resource.&lt;/p&gt;

&lt;p&gt;The values for Effect are &lt;code&gt;Allow&lt;/code&gt; and &lt;code&gt;Deny&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            ...
        },
        {
            "Effect": "Deny",
            ...
        }
    ]
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Action
&lt;/h3&gt;

&lt;p&gt;The Action element specifies the specific actions that are allowed or denied for the resource.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:PutObject"
            ],
            ...
        },
        {
            "Effect": "Deny",
            "Action": "ec2:*",
            ...
        }
    ]
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Resource
&lt;/h3&gt;

&lt;p&gt;The Resource element specifies the AWS resource that the policy applies to.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::example-bucket/*",
            ...
        },
        {
            "Effect": "Deny",
            "Action": "ec2:*",
            "Resource": "*",
            ...
        }
    ]
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Principal
&lt;/h3&gt;

&lt;p&gt;The Principal element specifies the entity that the policy applies to.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::example-bucket/*",
            "Principal": {
                "AWS": [
                    "arn:aws:iam::123456789012:user/Bob",
                    "arn:aws:iam::123456789012:role/DevOps"
                ]
            },
            ...
        },
        {
            "Effect": "Deny",
            "Action": "ec2:*",
            "Resource": "*",
            "Principal": "*",
            ...
        }
    ]
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What is an IAM Role in AWS?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;An IAM role is an AWS identity with permission policies that determine what the identity can and cannot do in AWS&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;IAM roles are similar to IAM users, but with some important differences.&lt;/p&gt;

&lt;p&gt;IAM roles are not associated with a specific user or group. Instead, they are intended to be assumed by anyone who needs the permissions associated with the role.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IAM roles are a way to delegate access to AWS resources without the need to create and manage long-term AWS credentials&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, you might create an IAM role that grants access to an S3 bucket, and then allow a Lambda function to assume that role when it needs to access the bucket.&lt;/p&gt;

&lt;p&gt;This way, you don't need to manage access keys for the Lambda function, and you can maintain tighter control over who has access to the S3 bucket.&lt;/p&gt;

&lt;h3&gt;
  
  
  Main Elements of an IAM Role
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Element&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Role Name&lt;/td&gt;
&lt;td&gt;The name assigned to the role.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Role ARN&lt;/td&gt;
&lt;td&gt;The Amazon Resource Name (ARN) assigned to the role.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Assume Role Policy&lt;/td&gt;
&lt;td&gt;The policy document that grants permission to assume the role.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Inline Policies&lt;/td&gt;
&lt;td&gt;One or more inline policies that are attached to the role.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Managed Policies&lt;/td&gt;
&lt;td&gt;One or more managed policies that are attached to the role.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Assuming an AWS IAM Role
&lt;/h2&gt;

&lt;p&gt;Assuming an AWS IAM Role means &lt;strong&gt;temporarily taking on the permissions and policies associated with that role&lt;/strong&gt;. This is done by using the &lt;strong&gt;AWS Security Token Service (STS)&lt;/strong&gt; API to obtain temporary security credentials, which include an access key, a secret access key, and a security token.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;user who is assuming the role must have permission to do so&lt;/strong&gt;, which is granted &lt;strong&gt;by the role's trust policy&lt;/strong&gt;. The trust policy specifies which users or services are allowed to assume the role.&lt;/p&gt;

&lt;p&gt;There are several ways to assume an IAM Role in AWS, including using the AWS Management Console, AWS CLI, AWS SDKs, and the AssumeRole API.&lt;/p&gt;

&lt;p&gt;When using the &lt;strong&gt;AssumeRole API, the user must provide their own security credentials and the ARN of the role they want to assume&lt;/strong&gt;. The API then returns temporary security credentials that can be used to access AWS resources associated with the role.&lt;/p&gt;

&lt;p&gt;Once the user has obtained temporary security credentials, they can use them to access AWS resources associated with the role until the credentials expire. &lt;strong&gt;The length of time that the credentials are valid can be set in the role's session duration policy. By default, this is set to one hour but can be increased up to 12 hours&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How is an IAM Policy different from an IAM Role?
&lt;/h2&gt;

&lt;p&gt;An IAM policy and an IAM role are both used to control access to AWS resources. However, they serve different purposes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;An IAM policy is used to define permissions&lt;/strong&gt; for a specific AWS resource or set of resources. It is &lt;strong&gt;attached to a user, group, or role&lt;/strong&gt; to &lt;strong&gt;grant or deny access&lt;/strong&gt; to those resources. &lt;/p&gt;

&lt;p&gt;IAM policies &lt;strong&gt;are static and do not change based on the user, group, or role&lt;/strong&gt; that is accessing the resources.&lt;/p&gt;

&lt;p&gt;In contrast, &lt;strong&gt;an IAM role is a set of permissions that can be assumed by a user, group, or service&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It is a way to delegate access to AWS resources without the need to share long-term security credentials such as access keys&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;When a &lt;strong&gt;user&lt;/strong&gt; (or a service) assumes an IAM role, they inherit the permissions associated with the role. &lt;strong&gt;IAM roles are dynamic&lt;/strong&gt; (opposed to policies) and can change based on the user, group, or service that is assuming the role.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IAM roles are useful for scenarios where you need to grant temporary access to a resource&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;For example, you might have a script that needs to access an S3 bucket to upload a file. Instead of embedding AWS credentials in the script, you can create an IAM role that has permission to access the S3 bucket and then assume that role in the script.&lt;/p&gt;

&lt;h2&gt;
  
  
  Examples of IAM Policies
&lt;/h2&gt;

&lt;p&gt;Here are some examples of simple IAM policies for common use cases:&lt;/p&gt;

&lt;h3&gt;
  
  
  Allow read-only access to an S3 bucket
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::example-bucket",
                "arn:aws:s3:::example-bucket/*"
            ]
        }
    ]
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This policy allows a user to read objects from the &lt;code&gt;example-bucket&lt;/code&gt; S3 bucket and list the contents of the bucket, but does not allow them to upload or modify objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Allow full access to an EC2 instance
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "ec2:*",
            "Resource": "*"
        }
    ]
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This policy allows a user to perform any action on any EC2 instance in the account.&lt;/p&gt;

&lt;h3&gt;
  
  
  Allow read-only access to an RDS instance
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "rds:DescribeDBInstances",
                "rds:DescribeDBSnapshots",
                "rds:DownloadDBLogFilePortion"
            ],
            "Resource": [
                "arn:aws:rds:us-west-2:123456789012:db:mysql-db",
                "arn:aws:rds:us-west-2:123456789012:snapshot:mysql-db-snapshot"
            ]
        }
    ]
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This policy allows a user to view information about the &lt;code&gt;mysql-db&lt;/code&gt; RDS instance and download log files, but does not allow them to perform any modifications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Allow read-only access to an SQS queue
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "sqs:GetQueueUrl",
                "sqs:ReceiveMessage"
            ],
            "Resource": "arn:aws:sqs:us-west-2:123456789012:example-queue"
        }
    ]
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This policy allows a user to retrieve the URL of the &lt;code&gt;example-queue&lt;/code&gt; SQS queue and receive messages from the queue, but does not allow them to send messages or modify the queue in any way.&lt;/p&gt;

&lt;h3&gt;
  
  
  Allow read-only access to a DynamoDB table
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "dynamodb:DescribeTable",
                "dynamodb:GetItem",
                "dynamodb:Query",
                "dynamodb:Scan"
            ],
            "Resource": "arn:aws:dynamodb:us-west-2:123456789012:table/example-table"
        }
    ]
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This policy allows a user to view the schema of the &lt;code&gt;example-table&lt;/code&gt; DynamoDB table and read items from the table, but does not allow them to modify the table in any way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Increase AWS Security Posture with IAM Roles and Policies
&lt;/h2&gt;

&lt;p&gt;Using a combination of IAM roles and policies is an effective way to &lt;strong&gt;increase the security posture&lt;/strong&gt; of an AWS account. Here are some best practices to follow:&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Least Privilege
&lt;/h3&gt;

&lt;p&gt;When creating IAM policies, it's important to &lt;strong&gt;use the principle of least privilege&lt;/strong&gt;. This means granting users and roles &lt;strong&gt;only the permissions they need to perform their job functions and no more&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, if a user only needs read access to an S3 bucket, don't grant them write or delete access.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rotate Credentials
&lt;/h3&gt;

&lt;p&gt;AWS recommends &lt;strong&gt;regularly rotating access keys and secret access keys for IAM users and roles&lt;/strong&gt;. This helps to reduce the risk of unauthorized access to your resources in the event that credentials are compromised.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use IAM Roles Instead of Access Keys
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;IAM roles are a more secure way to grant temporary access to AWS resources than using access keys&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When a user assumes an IAM role, they get temporary security credentials that expire after a specified amount of time. This minimizes the risk of credentials being compromised and reduces the need to manage long-term access keys.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Managed Policies
&lt;/h3&gt;

&lt;p&gt;AWS provides a number of managed policies that can be used to grant permissions to users and roles.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;These policies are created and maintained by AWS and are designed to follow best practices for security and compliance&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;By using managed policies, you can be sure that your users and roles have the appropriate permissions without having to create and maintain policies yourself.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Conditions in IAM Policies
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;IAM policies can include conditions that further restrict the permissions granted to users and roles&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, you can create a policy that only allows access to a resource during certain hours of the day or only from certain IP addresses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using conditions can help to further reduce the risk of unauthorized access&lt;/strong&gt; to your resources.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Multi-Factor Authentication (MFA)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Enabling multi-factor authentication (MFA) for IAM roles is an effective way to increase the security&lt;/strong&gt; of your AWS account.&lt;/p&gt;

&lt;p&gt;MFA requires users to provide a second form of authentication, such as a hardware token or a mobile app, in addition to their password.&lt;/p&gt;

&lt;p&gt;This makes it much more difficult for an attacker to gain unauthorized access to your resources even if they have obtained the user's password.&lt;/p&gt;

&lt;p&gt;By following these best practices, you can significantly increase the security posture of your AWS account and better protect your resources from unauthorized access.&lt;/p&gt;

&lt;h2&gt;
  
  
  What have we seen
&lt;/h2&gt;

&lt;p&gt;In this article, we explored the differences and similarities between IAM roles and policies in AWS.&lt;/p&gt;

&lt;p&gt;IAM policies define permissions for AWS resources and are used to grant or deny access to specific resources in your AWS account. &lt;/p&gt;

&lt;p&gt;IAM roles, on the other hand, are AWS identities with permission policies that determine what the identity can and cannot do in AWS. IAM roles are a way to delegate access to AWS resources without the need to create and manage long-term AWS credentials.&lt;/p&gt;

&lt;p&gt;We also discussed the main elements of an IAM policy, including the version, statement, effect, action, resource, and principal. Additionally, we explained how to assume an IAM role in AWS using the AWS Security Token Service (STS) API.&lt;/p&gt;

&lt;p&gt;To increase the security posture of an AWS account, we recommended using a combination of IAM roles and policies and following best practices such as using least privilege, rotating credentials, using managed policies, using conditions in IAM policies, and enabling multi-factor authentication (MFA) for IAM roles.&lt;/p&gt;

&lt;p&gt;Overall, by understanding the differences between IAM roles and policies and following best practices, you will be better equipped to manage access and identity needs on the AWS platform.&lt;/p&gt;

&lt;p&gt;I really hope that this little article has been of help for all of you, and as always, feel free to comment and reach us on our &lt;a href="https://join.slack.com/t/noovolari/shared_invite/zt-opn8q98k-HDZfpJ2_2U3RdTnN~u_B~Q"&gt;community slack&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Until next time, stay safe and see you in the next article, in which we’ll discuss about IAM Users and Groups! 😉&lt;/p&gt;

</description>
      <category>iam</category>
      <category>aws</category>
      <category>security</category>
      <category>iampolicies</category>
    </item>
    <item>
      <title>AWS IAM CLI: a cheatsheet</title>
      <dc:creator>Alessandro Gaggia</dc:creator>
      <pubDate>Wed, 21 Jun 2023 14:32:49 +0000</pubDate>
      <link>https://dev.to/aws-builders/aws-iam-cli-a-cheatsheet-3g7o</link>
      <guid>https://dev.to/aws-builders/aws-iam-cli-a-cheatsheet-3g7o</guid>
      <description>&lt;p&gt;&lt;a href="https://aws.amazon.com/cli/" rel="noopener noreferrer"&gt;AWS CLI&lt;/a&gt; stands for Amazon Web Services Command Line Interface.&lt;/p&gt;

&lt;p&gt;It is an open-source tool, and knowing how to use it to interact with AWS Services is crucial, especially for Developers. &lt;/p&gt;

&lt;p&gt;It allows to centralize control of all existing services from a single tool, and moreover, to make automated scripts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://aws.amazon.com/iam/" rel="noopener noreferrer"&gt;AWS Identity &amp;amp; Access Management&lt;/a&gt;, IAM in short, provides fine-grained access control across AWS services. &lt;/p&gt;

&lt;p&gt;This article will show how to use the &lt;a href="https://aws.amazon.com/cli/" rel="noopener noreferrer"&gt;AWS CLI&lt;/a&gt; to perform all the most common IAM operations.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites and Tips
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;If you haven't installed the AWS CLI yet, start by looking at &lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html" rel="noopener noreferrer"&gt;Installing the AWS CLI Guide from Amazon&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://jqlang.github.io/jq/" rel="noopener noreferrer"&gt;Download&lt;/a&gt; &lt;strong&gt;jq,&lt;/strong&gt; a lightweight and flexible &lt;strong&gt;JSON processor&lt;/strong&gt; for your terminal. 
Highly recommend  for &lt;strong&gt;automated script with the AWS CLI.&lt;/strong&gt; Look at the &lt;a href="https://jqlang.github.io/jq/" rel="noopener noreferrer"&gt;site&lt;/a&gt; for more information.&lt;/li&gt;
&lt;li&gt;Get the AWS CLI version: &lt;code&gt;$ aws --version&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1x1o1qz9r90b5508oiom.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1x1o1qz9r90b5508oiom.png" alt="AWS Version"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get the AWS CLI installation path: &lt;code&gt;$ which aws&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdds7jozwrefdj3709tx9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdds7jozwrefdj3709tx9.png" alt="AWS CLI Path"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Configure the AWS CLI for the first time: follow our &lt;a href="https://blog.leapp.cloud/how-to-use-aws-named-profiles" rel="noopener noreferrer"&gt;previous article&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$ aws --cli-auto-prompt&lt;/code&gt;: enable Auto Completion mode for the CLI, giving you suggestions as you write down your commands. Just remember to exit this mode when you need to run scripts!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS CLI: Versions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Version 2.x —&lt;/strong&gt; &lt;strong&gt;Used primarily for production environments&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Version 1.x —&lt;/strong&gt; Now available only for &lt;strong&gt;backward compatibility&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS CLI: command anatomy
&lt;/h2&gt;

&lt;p&gt;Users can create commands in &lt;strong&gt;single&lt;/strong&gt; or &lt;strong&gt;multiple&lt;/strong&gt; lines. The &lt;code&gt;\&lt;/code&gt; character splits a command into &lt;strong&gt;multiple lines for better readability&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In general, a command is structured in this way:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbfr7pszz2d6ezfs7055i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbfr7pszz2d6ezfs7055i.png" alt="AWS CLI Command description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You have the &lt;strong&gt;CLI invocation&lt;/strong&gt;, and then you &lt;strong&gt;apply a command to a specific service&lt;/strong&gt;. You can also add many different &lt;strong&gt;optional parameters&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;AWS IAM CLI: table of content&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;There are many different commands that you can exploit using the AWS CLI, but this article will focus only on those related to &lt;strong&gt;IAM&lt;/strong&gt; and &lt;strong&gt;STS&lt;/strong&gt; (AWS &lt;strong&gt;S&lt;/strong&gt;ecurity &lt;strong&gt;T&lt;/strong&gt;oken &lt;strong&gt;S&lt;/strong&gt;ervice). &lt;/p&gt;

&lt;p&gt;Because commands can have many optional parameters, we recommend opening this &lt;a href="https://docs.aws.amazon.com/cli/latest/reference/" rel="noopener noreferrer"&gt;link&lt;/a&gt; in your browser for further reference information.&lt;/p&gt;

&lt;p&gt;Below is a table of content you can use to navigate to a specific command.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prerequisites and Tips&lt;/li&gt;
&lt;li&gt;AWS CLI: Versions&lt;/li&gt;
&lt;li&gt;AWS CLI: command anatomy&lt;/li&gt;
&lt;li&gt;AWS IAM CLI: table of content&lt;/li&gt;
&lt;li&gt;AWS IAM CLI: create user&lt;/li&gt;
&lt;li&gt;AWS IAM CLI: list users&lt;/li&gt;
&lt;li&gt;AWS IAM CLI: update user&lt;/li&gt;
&lt;li&gt;AWS IAM CLI: delete user&lt;/li&gt;
&lt;li&gt;AWS IAM CLI: create IAM policy&lt;/li&gt;
&lt;li&gt;AWS IAM CLI: list IAM policies&lt;/li&gt;
&lt;li&gt;AWS IAM CLI: update IAM policy&lt;/li&gt;
&lt;li&gt;AWS IAM CLI: delete IAM policy&lt;/li&gt;
&lt;li&gt;AWS IAM CLI: create IAM role&lt;/li&gt;
&lt;li&gt;AWS IAM CLI: delete a Role&lt;/li&gt;
&lt;li&gt;AWS IAM CLI: attach policy to a User&lt;/li&gt;
&lt;li&gt;AWS IAM CLI: attach policy to an IAM role&lt;/li&gt;
&lt;li&gt;AWS IAM CLI: list all policies attached to a user&lt;/li&gt;
&lt;li&gt;AWS IAM CLI: list all policies attached to a role&lt;/li&gt;
&lt;li&gt;AWS IAM CLI: jq snippets&lt;/li&gt;
&lt;li&gt;Conclusions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS IAM CLI: &lt;strong&gt;create user&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Create a new IAM user.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;aws iam create-user --user-name AlessandroArticle&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;iam&lt;/code&gt;: Service&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;create-user&lt;/code&gt;: Command&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--user-name&lt;/code&gt;: Name of the user&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

{
    "User": {
        "Path": "/",
        "UserName": "AlessandroArticle",
        "UserId": "&amp;lt;user_id&amp;gt;",
        "Arn": "arn:aws:iam::&amp;lt;account_number&amp;gt;:user/AlessandroArticle",
        "CreateDate": "&amp;lt;creation_date&amp;gt;"
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS IAM CLI: &lt;strong&gt;list users&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Lists all users in the credentials’ set account.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;aws iam list-users&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;iam&lt;/code&gt;: Service&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;list-users&lt;/code&gt;: Command&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

{
    "Users": [
        {
            "Path": "/",
                "UserName": "AlessandroArticle",
                "UserId": "&amp;lt;user_id&amp;gt;",
                "Arn": "arn:aws:iam::&amp;lt;account_number&amp;gt;:user/AlessandroArticle",
                "CreateDate": "&amp;lt;creation_date&amp;gt;"
        }
        ]
}



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS IAM CLI: &lt;strong&gt;update user&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Updates an IAM user. We can update the name of a user using the &lt;code&gt;update-user&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;aws iam update-user --user-name AlessandroArticle --new-user-name AlessandroArticleNew&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;iam&lt;/code&gt;: Service&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;update-user&lt;/code&gt;: Command&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;—-user-name&lt;/code&gt;: The old name&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;—-new-user-name&lt;/code&gt;: The new name&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS IAM CLI: &lt;strong&gt;delete user&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Deletes the specified IAM user. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;aws iam delete-user —user-name AlessandroArticle&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;iam&lt;/code&gt;: Service&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;update-user&lt;/code&gt;: Command&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;—-user-name&lt;/code&gt;: The name of the user to remove&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note: you must delete the items attached to the user before attempting to delete a user, otherwise the command will fail (as per &lt;a href="https://docs.aws.amazon.com/cli/latest/reference/iam/delete-user.html" rel="noopener noreferrer"&gt;AWS documentation&lt;/a&gt;):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Password ( DeleteLoginProfile )&lt;/li&gt;
&lt;li&gt;Access keys ( DeleteAccessKey )&lt;/li&gt;
&lt;li&gt;Signing certificate ( DeleteSigningCertificate )&lt;/li&gt;
&lt;li&gt;SSH public key ( DeleteSSHPublicKey )&lt;/li&gt;
&lt;li&gt;Git credentials ( DeleteServiceSpecificCredential )&lt;/li&gt;
&lt;li&gt;Multi-factor authentication (MFA) device ( DeactivateMFADevice , DeleteVirtualMFADevice )&lt;/li&gt;
&lt;li&gt;Inline policies ( DeleteUserPolicy )&lt;/li&gt;
&lt;li&gt;Attached managed policies ( DetachUserPolicy )&lt;/li&gt;
&lt;li&gt;Group memberships ( RemoveUserFromGroup )&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Pro tips:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;List userId and UserName&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam list-users | jq -r ‘.Users[ ]|.UserId+” “+.UserName’


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Get single user&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam get-user --user-name (username)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Add user&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam create-user --user-name (username)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Delete user&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam delete-user --user-name (username)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;List access keys for user&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam list-access-keys --user-name (username) | jq -r .AccessKeyMetadata[ ].AccessKeyId


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Delete access key for user&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam delete-access-key --user-name (username) --access-key-id (accessKeyID)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Activate/deactivate access key for user&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam update-access-key --status Active --user-name (username) --access-key-id (access key)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam update-access-key --status Inactive --user-name (username) --access-key-id (access key)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Generate new access key for user&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam create-access-key --user-name (username) | jq -r ‘.AccessKey | .AccessKeyId+” “+.SecretAccessKey’


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS IAM CLI: &lt;strong&gt;create IAM policy&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Creates a new &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html" rel="noopener noreferrer"&gt;IAM policy&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;aws iam create-policy --policy-name example-policy --policy-document file://example-policy.json&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;iam&lt;/code&gt;: Service&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;create-policy&lt;/code&gt;: Command&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--policy-name&lt;/code&gt;: Name of the IAM policy&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--policy-document&lt;/code&gt;: Policy document in JSON format (useful because the policies are structured files)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An example policy document:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

{
    "Version": "2012-10-17",
    "Statement": [
        {
              "Effect": "Allow",
              "Action": [
                "s3:GetBucketLocation",
                "s3:ListBucket",
                "s3:ListBucketMultipartUploads"
              ],
              "Resource": [
                "arn:aws:s3:::&amp;lt;my_bucket&amp;gt;"
              ]
            }
    ]
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight idris"&gt;&lt;code&gt;

&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="s"&gt;"Policy"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="s"&gt;"PolicyName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"example-policy"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="s"&gt;"PolicyId"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;policy_id&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="s"&gt;"Arn"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"arn:aws:iam::&amp;lt;account_number&amp;gt;:policy/example-policy"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="s"&gt;"Path"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="s"&gt;"DefaultVersionId"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"v1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="s"&gt;"AttachmentCount"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mf"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="s"&gt;"PermissionsBoundaryUsageCount"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mf"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="s"&gt;"IsAttachable"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="s"&gt;"CreateDate"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;creation_date&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="s"&gt;"UpdateDate"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;update_date&amp;gt;"&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS IAM CLI: &lt;strong&gt;list IAM policies&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Lists IAM policies in the account.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;aws iam list-policies --scopes All&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;iam&lt;/code&gt;: Service&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;list-policies&lt;/code&gt;: Command&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--scopes&lt;/code&gt;: Policies scope. Possible values: &lt;code&gt;All&lt;/code&gt;, &lt;code&gt;AWS&lt;/code&gt;, &lt;code&gt;Local&lt;/code&gt;. &lt;strong&gt;AWS&lt;/strong&gt; is for managed policies, while &lt;strong&gt;Local&lt;/strong&gt; for custom policies.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Output&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

{
    "Policies": [
        {
           "Policy": {
              "PolicyName":"example-policy",
              "PolicyId":"&amp;lt;policy_id&amp;gt;",
              "Arn":"arn:aws:iam::&amp;lt;account_number&amp;gt;:policy/example-policy",
              "Path":"/",
              "DefaultVersionId":"v1",
              "AttachmentCount":0,
              "PermissionsBoundaryUsageCount":0,
              "IsAttachable":true,
              "CreateDate":"&amp;lt;creation_date&amp;gt;",
              "UpdateDate":"&amp;lt;update_date&amp;gt;"
           }
        }
    ]
}



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS IAM CLI: &lt;strong&gt;update IAM policy&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Edit an IAM policy and set it as default.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam create-policy-version \
 --policy-arn arn:aws:iam::123456789012:policy/my-policy \
 --policy-document file://NewPolicyVersion.json --set-as-default


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;iam&lt;/code&gt;: Service&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;create-policy-version&lt;/code&gt;: Command&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--policy-arn&lt;/code&gt;: ARN of the policy&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--policy-document&lt;/code&gt;: Updated policy file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS IAM CLI: &lt;strong&gt;delete IAM policy&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Delete a policy given the ARN.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;aws iam delete-policy --policy-arn arn**:**aws**:**iam**::**123456789012**:**policy/my-policy&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;iam&lt;/code&gt;: Service&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;delete-policy&lt;/code&gt;: Command&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--policy-arn&lt;/code&gt;: ARN of the policy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS IAM CLI: &lt;strong&gt;create IAM role&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Creates a new IAM role. The arguments for this command are:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;aws iam create-role --role-name example-role --assume-role-policy-document file://assume-policy.json&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;iam&lt;/code&gt;: Service&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;create-role&lt;/code&gt;: Command&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--role-name&lt;/code&gt;: Name of the IAM role&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--assume-role-policy-document&lt;/code&gt;: Trust relationship policy document that grants an entity permission to assume the role&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this example, we will create an IAM role that grants AWS Glue permission to assume the role (as &lt;a href="https://blog.leapp.cloud/what-is-an-iam-principal" rel="noopener noreferrer"&gt;principal&lt;/a&gt;).&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

{
    "Version": "2012-10-17",
    "Statement": [
        {
        "Effect": "Allow",
        "Principal": {
        "Service": "glue.amazonaws.com"
        },
        "Action": "sts:AssumeRole"
        }
    ]
}



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

{
    "Role": {
        "Path": "/",
        "RoleName": "example-role",
        "RoleId": "&amp;lt;role_id&amp;gt;",
        "Arn": "arn:aws:iam::&amp;lt;account_number&amp;gt;:role/example-role",
        "CreateDate": "&amp;lt;creation_date&amp;gt;",
        "AssumeRolePolicyDocument": {
            "Version": "2012-10-17",
            "Statement": [
                {
                    "Effect": "Allow",
                    "Principal": {
                        "Service": "glue.amazonaws.com"
                    },
                    "Action": "sts:AssumeRole"
                }
            ]
        }
    }
}



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS IAM CLI: &lt;strong&gt;delete a Role&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Deletes an IAM Role.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;aws iam delete-role --role-name Test-Role&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;iam&lt;/code&gt;: Service&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;delete-role&lt;/code&gt;: Command&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--role-name&lt;/code&gt;: Name of the IAM Role to remove&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS IAM CLI: &lt;strong&gt;attach policy to a User&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To allow a User to do some actions, apply a policy to it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;aws iam attach-user-policy --user-name AlessandroArticle --policy-arn arn:aws:iam::&amp;lt;policy_id&amp;gt;:policy/my-policy&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;iam&lt;/code&gt;: Service&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;attach-user-policy&lt;/code&gt;: Command&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--user-name&lt;/code&gt;: Name of the IAM user&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--policy-arn&lt;/code&gt;: ARN of the IAM policy to attach&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this example, we will attach the IAM policy we created earlier to an example IAM.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS IAM CLI: &lt;strong&gt;attach policy to an IAM role&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;We can also attach a policy to a IAM role.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;aws iam attach-role-policy --role-name example-role --policy-arn arn:aws:iam::&amp;lt;policy_id&amp;gt;:policy/my-policy&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;iam&lt;/code&gt;: Service&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;attach-role-policy&lt;/code&gt;: Command&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--role-name&lt;/code&gt;: Name of the IAM role&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--policy-arn&lt;/code&gt;: ARN of the IAM policy you want to attach&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS IAM CLI: &lt;strong&gt;list all policies attached to a user&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;We can list all policies attached to an IAM User.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;aws iam list-attached-user-policies --user-name AlessandroArticle&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;iam&lt;/code&gt;: Service&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;list-attached-user-policies&lt;/code&gt;: Command&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--user-name&lt;/code&gt;: The User to whom the policies are attached to&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Output&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight idris"&gt;&lt;code&gt;

&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"AttachedPolicies"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="s"&gt;"PolicyName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"my-policy"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s"&gt;"PolicyArn"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"arn:aws:iam::&amp;lt;account_number&amp;gt;:policy/learnaws-dynamo-policy"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS IAM CLI: &lt;strong&gt;list all policies attached to a role&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;List all policies attached to an IAM Role.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;aws iam list-attached-role-policies --role-name example-role&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;iam&lt;/code&gt;: Service&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;list-attached-role-policies&lt;/code&gt;: Command&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--role-name&lt;/code&gt;: The Role to whom the policies are attached to&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Output&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight idris"&gt;&lt;code&gt;

&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"AttachedPolicies"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="s"&gt;"PolicyName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"my-policy"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s"&gt;"PolicyArn"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"arn:aws:iam::&amp;lt;account_number&amp;gt;:policy/example-policy"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"UserId"&lt;/span&gt;: &lt;span class="s2"&gt;"AROAJQ3ISEWFFR6GXAW:&amp;lt;user_name&amp;gt;"&lt;/span&gt;,
    &lt;span class="s2"&gt;"Account"&lt;/span&gt;: &lt;span class="s2"&gt;"637004329899"&lt;/span&gt;,
    &lt;span class="s2"&gt;"Arn"&lt;/span&gt;: &lt;span class="s2"&gt;"arn:aws:sts::637004329899:assumed-role/&amp;lt;role-name&amp;gt;/&amp;lt;user_name&amp;gt;"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS IAM CLI: &lt;strong&gt;jq snippets&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Finally, thanks to the excellent &lt;a href="https://www.bluematador.com/learn/aws-cli-cheatsheet" rel="noopener noreferrer"&gt;tutorial from BlueMatador&lt;/a&gt;, here we present some fast snippets that integrate the *&lt;strong&gt;&lt;em&gt;jq&lt;/em&gt;&lt;/strong&gt;* tool to extrapolate useful info for different use-cases. Kudos to them 🙂.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;List groups&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam list-groups | jq -r .Groups[ ].GroupName


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Add/Delete groups&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam create-group --group-name (groupName)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;List policies and ARNs&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam list-policies | jq -r ‘.Policies[ ]|.PolicyName+” “+.Arn’


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam list-policies --scope AWS | jq -r ‘.Policies[ ]|.PolicyName+” “+.Arn’


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam list-policies --scope Local | jq -r ‘.Policies[ ]|.PolicyName+” “+.Arn’


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;List user/group/roles for a policy&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam list-entities-for-policy --policy-arn arn:aws:iam:2308345:policy/example-ReadOnly


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;List policies for a group&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam list-attached-group-policies --group-name (groupname)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Add policy to a group&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam attach-group-policy --group-name (groupname) --policy-arn arn:aws:iam::aws:policy/exampleReadOnlyAccess


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Add user to a group&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam add-user-to-group --group-name (groupname) --user-name (username)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Remove user from a group&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam remove-user-from-group --group-name (groupname) --user-name (username)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;List users in a group&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam get-group --group-name (groupname)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;List groups for a user&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam list-groups-for-user --user-name (username)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Attach/detach policy to a group&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam attach-group-policy --group-name (groupname) --policy-arn arn:aws:iam::aws:policy/DynamoDBFullAccess


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

aws iam detach-group-policy --group-name (groupname) --policy-arn arn:aws:iam::aws:policy/DynamoDBFullAccess


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&amp;lt;!-- TOC --&amp;gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;This article shows that AWS CLI is a powerful tool for automatic operations on AWS services. &lt;/p&gt;

&lt;p&gt;In particular, we have used IAM and STS services to explore all the different commands that we can leverage for Access Management and Identity governance.&lt;/p&gt;

&lt;p&gt;We have demonstrated that AWS CLI commands can be chained with other terminal tools to push even further your automation scripts.&lt;/p&gt;

&lt;p&gt;Finally, we have seen how &lt;strong&gt;jq&lt;/strong&gt; can be a perfect companion for the CLI to obtain properties out of JSON-formatted files or command results.&lt;/p&gt;

&lt;p&gt;If this article interested you, next week we will continue with a new cheatsheet correlated to STS and how it is tied closely to our open-source tool &lt;a href="https://leapp.cloud/" rel="noopener noreferrer"&gt;Leapp&lt;/a&gt;. Don’t miss it out!&lt;/p&gt;

&lt;p&gt;Thank you everyone, for coming this far. We hope that you enjoyed this little “cheatsheet”. &lt;/p&gt;

&lt;p&gt;As always, if you have questions, clarifications, or just want to share your opinions, feel free to join our &lt;strong&gt;&lt;a href="https://join.slack.com/t/noovolari/shared_invite/zt-opn8q98k-HDZfpJ2_2U3RdTnN~u_B~Q" rel="noopener noreferrer"&gt;Top of the Ops community&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Until next time, stay safe 🙂!&lt;/p&gt;

</description>
      <category>aws</category>
      <category>iam</category>
      <category>cli</category>
      <category>security</category>
    </item>
    <item>
      <title>What is a IAM Principal?</title>
      <dc:creator>Alessandro Gaggia</dc:creator>
      <pubDate>Wed, 07 Jun 2023 13:06:45 +0000</pubDate>
      <link>https://dev.to/aws-builders/what-is-a-iam-principal-3a1p</link>
      <guid>https://dev.to/aws-builders/what-is-a-iam-principal-3a1p</guid>
      <description>&lt;p&gt;Whenever you find yourself working with AWS access model, being a newbie or an experienced DevOps, there is a lot of terminology to digest and learn.&lt;/p&gt;

&lt;p&gt;Users, Roles and Policies are terms that we learn pretty early in our career, however there is one concept that has a lot of meaning behind, and, in my opinion, deserve a little more explanation: &lt;strong&gt;IAM Principal&lt;/strong&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  IAM Principal: definition
&lt;/h2&gt;

&lt;p&gt;Let’s start by giving a standard definition: &lt;em&gt;a principal is a human user or workload that can make a request for an action or operation on an AWS resource.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Moreover a principal is anything, AWS related, that can &lt;em&gt;send a request to AWS, via the Management Console, the AWS API, or the AWS CLI.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  IAM Principal: actors
&lt;/h2&gt;

&lt;p&gt;In AWS &lt;strong&gt;What&lt;/strong&gt; or &lt;strong&gt;Who&lt;/strong&gt; are the “actors” that can be referred as Principals? &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Root User&lt;/li&gt;
&lt;li&gt;IAM Users&lt;/li&gt;
&lt;li&gt;IAM Roles/Temporary Security Tokens&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The main differences among them are &lt;strong&gt;Who can impersonate them&lt;/strong&gt;, and &lt;strong&gt;What credentials they are granted&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Users&lt;/strong&gt; are impersonated by human users while &lt;strong&gt;Roles&lt;/strong&gt; by services, or workloads in general.&lt;/p&gt;

&lt;p&gt;Also, while the first are granted &lt;strong&gt;long-lived credentials&lt;/strong&gt;, Roles use &lt;strong&gt;temporary credentials&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Root user&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;The Root user is the owner of the AWS main Account, and it should be protected by &lt;strong&gt;MFA&lt;/strong&gt; and a &lt;strong&gt;strong password&lt;/strong&gt; right after registering. We &lt;strong&gt;should avoid using Root user as a Principal&lt;/strong&gt;, for security reasons, and instead use IAM Users and Roles.&lt;/p&gt;

&lt;p&gt;He accesses AWS with email and password: that is why we must avoid using it for operation work!&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;IAM Users&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An IAM user is a &lt;strong&gt;persistent&lt;/strong&gt; entity that is given a set of credentials to manage services on AWS. The IAM User can be associated with both long or short-lived credentials (tough short-lived ones are &lt;strong&gt;much preferred&lt;/strong&gt;). &lt;/p&gt;

&lt;p&gt;An IAM User can be authorised to perform actions  directly via an AWS Policy attached to it, or via a Group Policy.&lt;/p&gt;

&lt;p&gt;An IAM User is created through:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AWS Management Console&lt;/li&gt;
&lt;li&gt;CLI&lt;/li&gt;
&lt;li&gt;SDKs&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;IAM Role&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;a IAM Role is a Principal &lt;strong&gt;associated directly to AWS Resources&lt;/strong&gt; or assumed by a IAM User, the AWS CLI, or one of the AWS SDKs available. It has &lt;strong&gt;permissions granted via temporary credentials&lt;/strong&gt;. It has one or more Policies attached to it.&lt;/p&gt;

&lt;p&gt;Some use cases related to IAM Roles involve:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Amazon EC2 Roles&lt;/strong&gt;: grant permissions to &lt;strong&gt;applications running on an Amazon EC2 instance&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cross-Account Access&lt;/strong&gt;: grant permissions to &lt;strong&gt;users from other AWS accounts&lt;/strong&gt;, whether in control of those accounts or not.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Temporary Security Tokens:&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Temporary Security Tokens are obtained from the AWS Security Token Service (STS). &lt;/p&gt;

&lt;p&gt;They always have an expiration date and time, and have a lifespan between &lt;strong&gt;15 minutes&lt;/strong&gt; and &lt;strong&gt;up to 36 hours&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;These tokens are usually obtained through a &lt;strong&gt;Federation Process&lt;/strong&gt;, which involves granting permissions to users &lt;strong&gt;authenticated by a trusted external system&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here is a summary from AWS showing some types of Roles/Temporary Security Tokens and from what they are obtained:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Principal&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Docs &amp;amp; Links&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Federated User (Login via custom proxy architecture)&lt;/td&gt;
&lt;td&gt;FederatedUser&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Web Federated User (Login with Amazon, Amazon Cognito, Facebook, Google)&lt;/td&gt;
&lt;td&gt;AssumedRole&lt;/td&gt;
&lt;td&gt;&lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_oidc_user-id.html"&gt;https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_oidc_user-id.html&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SAML 2.0 Federated User&lt;/td&gt;
&lt;td&gt;AssumedRole&lt;/td&gt;
&lt;td&gt;&lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_saml.html#CreatingSAML-userid"&gt;https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_saml.html#CreatingSAML-userid&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;AWS SSO User&lt;/td&gt;
&lt;td&gt;AssumedRole&lt;/td&gt;
&lt;td&gt;&lt;a href="https://dev.to/aws-heroes/adding-aws-sso-and-controlling-permissions-56ga"&gt;https://dev.to/aws-heroes/adding-aws-sso-and-controlling-permissions-56ga&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A special mention goes to &lt;strong&gt;AWS SSO Users&lt;/strong&gt; that are &lt;strong&gt;configured via IAM Identity Center permission sets&lt;/strong&gt; to access different &lt;strong&gt;accounts with different roles&lt;/strong&gt;. When a user lands on an account &lt;strong&gt;a role is given transparently&lt;/strong&gt; to access services and resources according to the policies set up by the administrator.&lt;/p&gt;

&lt;p&gt;IAM Roles, AWS SSO, and Temporary Security Tokens are meant for advanced AWS IAM usage and have always a &lt;strong&gt;limited set duration of time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Because these Principals are &lt;strong&gt;extremely important and versatile&lt;/strong&gt;, I strongly advise reading more on the argument, starting from &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp.html"&gt;this article&lt;/a&gt; and also this &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_variables.html#principaltable"&gt;one&lt;/a&gt;, concluding with this &lt;a href="https://dev.to/aws-heroes/adding-aws-sso-and-controlling-permissions-56ga"&gt;one&lt;/a&gt; from AWS Hero &lt;strong&gt;&lt;a href="https://aws.amazon.com/it/developer/community/heroes/matt-lewis/"&gt;Matt Lewis&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  IAM Principal: authentication, requests, and authorization
&lt;/h2&gt;

&lt;p&gt;When working with Principals we must understand the two actions that must be taken to finally obtain a set of valid Credentials. &lt;/p&gt;

&lt;p&gt;First &lt;strong&gt;Authentication&lt;/strong&gt; of the subject against AWS or a valid Identity Provider, and then &lt;strong&gt;Authorization&lt;/strong&gt; to be able to perform actions and access specific resources.&lt;/p&gt;

&lt;h3&gt;
  
  
  IAM Principal: Authentication
&lt;/h3&gt;

&lt;p&gt;A Principal must always authenticate to send AWS requests. &lt;/p&gt;

&lt;p&gt;Only Amazon S3, SQS, SNS, and AWS STS allow limited (and specific) requests from anonymous users, &lt;strong&gt;but they're the exception&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;To log in as a &lt;strong&gt;Root User on the console&lt;/strong&gt;, use your &lt;strong&gt;email and password&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;As a &lt;strong&gt;Federated User&lt;/strong&gt;, you can access AWS resources through &lt;strong&gt;IAM roles granted by your identity provider's authentication&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Enter your Access Key ID  and Secret Access Key to authenticate as an &lt;strong&gt;IAM user&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;To authenticate workloads, use &lt;strong&gt;temporary&lt;/strong&gt; or long-term credentials (always preferring the first). &lt;/p&gt;

&lt;p&gt;DevOps’ best practices recommend using MFA and temporary credentials to keep your account secure. &lt;/p&gt;

&lt;h3&gt;
  
  
  IAM Principal: Request
&lt;/h3&gt;

&lt;p&gt;When a principal tries to do something with AWS using the AWS Management Console, an SDK, or the CLI, it sends a &lt;em&gt;request&lt;/em&gt; to AWS with the following information:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Actions or operations:&lt;/strong&gt; the actions to perform.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resources:&lt;/strong&gt; the AWS services or objects to manipulate.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Principal:&lt;/strong&gt; and now we know what they can be 😀!&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Environment data:&lt;/strong&gt; various metadata, like IP address, User agent, etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resource data:&lt;/strong&gt; metadata depending on the specific AWS resource.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AWS puts all of these in a &lt;em&gt;request context&lt;/em&gt;, which is used to evaluate and authorize the request.&lt;/p&gt;

&lt;h3&gt;
  
  
  IAM Principal: Authorization
&lt;/h3&gt;

&lt;p&gt;After Authentication, you must also be authorized to complete your request. &lt;/p&gt;

&lt;p&gt;During the authorization process, AWS uses the &lt;em&gt;request context&lt;/em&gt; to check for &lt;strong&gt;policies that conform to the request&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;It then &lt;strong&gt;parses the policies to determine whether to allow or deny the request&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Most policies are stored in AWS as &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html#access_policies-json"&gt;JSON documents&lt;/a&gt; and specify the permissions for principal entities. There are &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html"&gt;several types of policies&lt;/a&gt; that can affect the outcome of the request. &lt;/p&gt;

&lt;p&gt;If a single permissions policy &lt;strong&gt;includes a denied action&lt;/strong&gt;, &lt;strong&gt;AWS denies the entire request and stops evaluating&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Because requests are &lt;em&gt;denied by default&lt;/em&gt;, they can complete only if every part of the request is allowed by the permissions policies. &lt;/p&gt;

&lt;p&gt;The general rules to understand how the authorization works are the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;By default, all requests are denied&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;An explicit allow&lt;/strong&gt; (identity-based or resource-based) &lt;strong&gt;overrides the default deny&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Organizations SCPs, IAM permissions boundaries, or session policies overrides the allow&lt;/strong&gt;. But &lt;strong&gt;if there are more than one of this policies, they all need to allow the request&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;An &lt;strong&gt;explicit deny in any policy overrides any allows&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To learn more about how this process is evaluated, see &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_evaluation-logic.html"&gt;Policy evaluation logic&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  IAM Principal: Credentials
&lt;/h2&gt;

&lt;p&gt;As we have seen there are two types of credentials granted to a Principal: &lt;strong&gt;long-lived&lt;/strong&gt; and &lt;strong&gt;temporary short-lived&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Long-lived credentials last until an administrator &lt;strong&gt;explicitly removes them&lt;/strong&gt; from a Principals and usually, are applied to &lt;strong&gt;Root Users&lt;/strong&gt; and &lt;strong&gt;IAM Users/Groups&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Short-lived credentials are temporary&lt;/strong&gt;, and have an explicit expiration date and time. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;They are generated through AWS STS Service&lt;/strong&gt; and &lt;strong&gt;are associated with IAM Roles, Assumed Roles&lt;/strong&gt;, or even &lt;strong&gt;Federated Identities&lt;/strong&gt; (AWS SSO, Web, SAML, OIDC, OAuth, Cognito, Azure AD, etc.)&lt;/p&gt;

&lt;h2&gt;
  
  
  IAM Principal: Best Practices
&lt;/h2&gt;

&lt;p&gt;As short-lived credentials can also be applied to a Group or a User thanks to the Assume Role action, I &lt;strong&gt;strongly recommend adhering to DevOps best practices and always using short-lived credentials whenever possible.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;It is preferable to use temporary credentials &lt;strong&gt;because they reduce the potential damage done by an attacker stealing them&lt;/strong&gt; (they are limited in time).&lt;/p&gt;

&lt;p&gt;It is also savvy to &lt;strong&gt;narrow down policies associated with users and roles to the bare minimum set of resources&lt;/strong&gt; that you need for your work. Always take advantage of the Deny First rules!&lt;/p&gt;

&lt;p&gt;With the Introduction of &lt;strong&gt;IAM Identity Center is always preferable for administrators to use it for creating new Users&lt;/strong&gt; as they will also benefit from a landing panel to choose the account and role from.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sum Up
&lt;/h2&gt;

&lt;p&gt;In this short article, we learned about AWS IAM Principals, how they are categorized, and what they stand for.&lt;/p&gt;

&lt;p&gt;We have learned that IAM Principals have to deal with both an Authentication and an Authorization process to be able to operate successfully on AWS.&lt;/p&gt;

&lt;p&gt;We have seen what a request, made to AWS by a Principal, contains.&lt;/p&gt;

&lt;p&gt;We have seen what a Root account and an IAM User are, how they differ from IAM Roles, and moreover from Temporary Security Tokens.&lt;/p&gt;

&lt;p&gt;We have analysed the differences between Long and Short lived Credentials, and why the latter are preferable and more secure.&lt;/p&gt;

&lt;p&gt;We have proposed some basic best practices to follow to better use your AWS Principals.&lt;/p&gt;

&lt;p&gt;So, thank you all for reaching the end of the article!&lt;/p&gt;

&lt;p&gt;As always, if you have suggestions or questions, feel free to come and have a chat with us on our &lt;a href="https://join.slack.com/t/noovolari/shared_invite/zt-opn8q98k-HDZfpJ2_2U3RdTnN~u_B~Q"&gt;slack community&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Until next time, see ya and stay safe! 🙂&lt;/p&gt;

</description>
      <category>aws</category>
      <category>security</category>
      <category>governance</category>
      <category>iam</category>
    </item>
    <item>
      <title>How to fix response did not contain a valid saml assertion</title>
      <dc:creator>Alessandro Gaggia</dc:creator>
      <pubDate>Fri, 21 Apr 2023 12:31:18 +0000</pubDate>
      <link>https://dev.to/aws-builders/how-to-fix-response-did-not-contain-a-valid-saml-assertion-3khg</link>
      <guid>https://dev.to/aws-builders/how-to-fix-response-did-not-contain-a-valid-saml-assertion-3khg</guid>
      <description>&lt;p&gt;If you read this article, you are managing user identities outside of AWS and using Identity Provider (IdP) Federation to give these external identities permission to use AWS resources in your account.&lt;/p&gt;

&lt;p&gt;In this authentication process, one of the &lt;strong&gt;most common errors&lt;/strong&gt; you may need to confront is "response did not contain a valid saml assertion," and in this article, I want to share with you some troubleshooting advice to solve it.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Investigating a No valid assertion found in SAML response&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Checking the attribute name and attribute value on your IdP
&lt;/h3&gt;

&lt;p&gt;If you are on AWS (but in general), invalid SAML assertion mainly occurs when the SAML response from the IdP does not include an attribute with the &lt;code&gt;Name&lt;/code&gt; set to &lt;code&gt;https://aws.amazon.com/SAML/Attributes/Role&lt;/code&gt; . The attribute must also contain one or more &lt;code&gt;AttributeValue&lt;/code&gt; elements, &lt;strong&gt;each with these two strings&lt;/strong&gt;, separated by a comma:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The ARN of a role that the user can be mapped to&lt;/li&gt;
&lt;li&gt;The ARN of the SAML provider&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;E.g.:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;Attribute&lt;/span&gt; &lt;span class="na"&gt;Name=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;https://aws.amazon.com/SAML/Attributes/Role&amp;gt;&lt;/span&gt;"&amp;gt;

&lt;span class="s"&gt;&amp;lt;AttributeValue&amp;gt;&lt;/span&gt;arn:aws:iam::account-number:role/role-name1,arn:aws:iam::account-number:saml-provider/provider-name&lt;span class="s"&gt;&amp;lt;/AttributeValue&amp;gt;&lt;/span&gt;

&lt;span class="s"&gt;&amp;lt;/Attribute&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://repost.aws/knowledge-center/iam-invalid-saml-response-okta"&gt;Here&lt;/a&gt; is also an example of a resolution for Okta.&lt;/p&gt;

&lt;h3&gt;
  
  
  Time Synchronisation issues between IdP and Service Provider
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://docs.pulsesecure.net/WebHelp/Content/PCS/PCS_AdminGuide_8.2/Investigating%20a%20No%20valid%20assertion.htm"&gt;https://docs.pulsesecure.net/WebHelp/Content/PCS/PCS_AdminGuide_8.2/Investigating a No valid assertion.htm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If the SAML IdP and SAML service provider (AWS, for example) clocks are not synchronized, the assertion can be determined invalid&lt;/strong&gt;, and authentication fails.&lt;/p&gt;

&lt;p&gt;A possible solution is to verify that your IdP and Service provider &lt;strong&gt;can share the same NTP server&lt;/strong&gt; or prove that your server's clock is up-to-date.&lt;/p&gt;

&lt;h3&gt;
  
  
  Metadata.xml mismatch
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Data refreshes, and upgrades can cause the certificates to be no longer trusted by one side of the Federation process or the other&lt;/strong&gt;. Try to check and update the metadata.xml on both ends so the certificates will match again.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;SAML message not properly formatted, with missing or invalid elements&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/64158310/aws-sso-your-request-included-an-invalid-saml-response"&gt;https://stackoverflow.com/questions/64158310/aws-sso-your-request-included-an-invalid-saml-response&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sometimes the error occurs not only with the User attribute but in general if the message needs to include all the required information in the required format. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The message was signed, but &lt;strong&gt;the signature could not be verified&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Assertion contains an unacceptable &lt;strong&gt;Audience Restriction&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The assertion is no longer valid, or the message is expired, see &lt;strong&gt;Time Synchronisation issues between IdP and Service Provider&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SAML response contained an error&lt;/strong&gt; indicating that the Cloud Provider received a SAML message from an IdP with an error status code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Remember that SAML is schema-compliant, so you must adhere to its standard when creating its XML request. Make sure to refer to this &lt;a href="http://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf"&gt;document&lt;/a&gt; to see all the standard tags.&lt;/p&gt;

&lt;p&gt;Some more examples of possible typos can be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Not including encoding at the beginning of the XML:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Typo in the Recipient of the SubjectConfirmationData : set it to "&lt;a href="https://signin.aws.amazon.com/saml"&gt;https://signin.aws.amazon.com/saml&lt;/a&gt;."&lt;/li&gt;
&lt;li&gt;Not including an AuthnStatement.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;How to view a SAML response for troubleshooting&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/troubleshoot_saml.html"&gt;https://docs.aws.amazon.com/IAM/latest/UserGuide/troubleshoot_saml.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This article has several ideas to help you narrow down the exact cause of the issue. Still, I'd also like to give you some basic tips on debugging the SAML assertion you receive to find details that can point you to the root cause of your problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Google Chrome and Firefox&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Press &lt;strong&gt;F12&lt;/strong&gt; to start the &lt;strong&gt;Developer Tools&lt;/strong&gt; console.&lt;/li&gt;
&lt;li&gt;Select the &lt;strong&gt;Network&lt;/strong&gt; tab, and then select &lt;strong&gt;Preserve log&lt;/strong&gt; (Persist Log in Firefox)&lt;/li&gt;
&lt;li&gt;Look for a &lt;strong&gt;SAML Post&lt;/strong&gt;, then view the &lt;strong&gt;Payload&lt;/strong&gt; tab at the top. Look for the &lt;strong&gt;SAMLResponse&lt;/strong&gt; element that contains the Base64-encoded response.&lt;/li&gt;
&lt;li&gt;Copy it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;💡 Security Note: as SAML assertions contain sensitive information, I discourage you from using &lt;em&gt;online base64 decoders&lt;/em&gt; and using one of these simple scripts to do it from your local terminal.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Windows systems (PowerShell):&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;PS C:\\&amp;gt;[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String("base64encodedtext"))&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;MacOS and Linux systems:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;$echo "base64encodedtext" | base64 --decode&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Also, if the attributes from your Identity Provider are &lt;strong&gt;not encrypted&lt;/strong&gt;, the &lt;a href="https://addons.mozilla.org/en-US/firefox/addon/saml-tracer"&gt;Firefox browser SAML tracer Add-on&lt;/a&gt; or &lt;a href="https://chrome.google.com/webstore/detail/saml-message-decoder/mpabchoaimgbdbbjjieoaeiibojelbhm?hl=en"&gt;Chrome SAML Message Decoder&lt;/a&gt; can view these attributes.****&lt;/p&gt;

&lt;h3&gt;
  
  
  Tutorials
&lt;/h3&gt;

&lt;p&gt;To help you further, here are two articles from our blog where we share some hints to configure SAML with GSuite (note that concepts and properties are similar to other IdPs).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://blog.leapp.cloud/how-to-saml-federate-your-aws-account-with-g-suite"&gt;https://blog.leapp.cloud/how-to-saml-federate-your-aws-account-with-g-suite&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.leapp.cloud/how-to-update-in-bulk-g-suite-users-custom-attributes-with-google-admin-sdk"&gt;https://blog.leapp.cloud/how-to-update-in-bulk-g-suite-users-custom-attributes-with-google-admin-sdk&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;In this article, we have seen how to troubleshoot a very pesky error of the SAML Federation: "Response did not contain a valid SAML assertion."&lt;/p&gt;

&lt;p&gt;We have shown that it can occur when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Role attributes are not set correctly&lt;/strong&gt; in the SAML request - IdP side.&lt;/li&gt;
&lt;li&gt;There is a &lt;strong&gt;time desynchronization&lt;/strong&gt; between the IdP and the Service Provider.&lt;/li&gt;
&lt;li&gt;There is a &lt;strong&gt;Metadata.xml mismatch&lt;/strong&gt; between the actors, so the certificate doesn't match.&lt;/li&gt;
&lt;li&gt;There are &lt;strong&gt;typos or an invalid SAML structure&lt;/strong&gt; in your request.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In general, I always return to &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/troubleshoot_saml.html#troubleshoot_saml_invalid-response"&gt;this&lt;/a&gt; link when I need to troubleshoot a SAML response, as the problem may lie on a different configuration depending on the IdP you're using.&lt;/p&gt;

&lt;p&gt;This little article has been of help to all of you, and till next time, Happy SAML assertions, and see you in the next article! 😉&lt;/p&gt;

</description>
      <category>iam</category>
      <category>saml</category>
      <category>howtofix</category>
      <category>federation</category>
    </item>
    <item>
      <title>How to quickly automate AWS Federated Session generation with Leapp CLI</title>
      <dc:creator>Alessandro Gaggia</dc:creator>
      <pubDate>Thu, 21 Jul 2022 13:38:43 +0000</pubDate>
      <link>https://dev.to/aws-builders/how-to-quickly-automate-aws-federated-session-generation-with-leapp-cli-d2m</link>
      <guid>https://dev.to/aws-builders/how-to-quickly-automate-aws-federated-session-generation-with-leapp-cli-d2m</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Ever wanted to create and use your personal AWS Federation with Google as an IdP in a programmatic way? &lt;/p&gt;

&lt;p&gt;This short article will surely come to the rescue, introducing some of the features of Leapp CLI.&lt;/p&gt;

&lt;p&gt;Let’s start!&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;AWS Account access with adeguate IAM privileges&lt;/li&gt;
&lt;li&gt;Google Suite access with Admin privileges&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before creating a Federated session using Leapp CLI, we must set up our Google IdP in AWS. &lt;/p&gt;

&lt;p&gt;We can refer to our &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-idp_saml.html" rel="noopener noreferrer"&gt;guide&lt;/a&gt; as a guideline, but for this article, let’s review some important key values here.&lt;/p&gt;

&lt;p&gt;The AWS IAM Federation gives the ability to assume a role with permissions to do actions on AWS resources to an external user. This operation can generate temporary credentials for both programmatic access (CLI or SDKs) and for console access.&lt;/p&gt;

&lt;p&gt;We will focus on programmatic access.&lt;/p&gt;

&lt;p&gt;Trust between Google and AWS depends on 2 metadata files, one from AWS that must be passed to Google and one from Google that also contains keys that AWS can use to validate authentication responses (assertions) from your organization.&lt;/p&gt;

&lt;p&gt;Another useful guide to set up things on your side is &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_saml.html" rel="noopener noreferrer"&gt;this&lt;/a&gt; one from AWS, which shows how the Federation mechanism works, and &lt;a href="https://faun.pub/how-to-configure-google-saml-for-aws-account-5eb89e2d3008" rel="noopener noreferrer"&gt;this&lt;/a&gt; one from *&lt;em&gt;**&lt;a href="https://krish512.medium.com/?source=post_page-----5eb89e2d3008--------------------------------" rel="noopener noreferrer"&gt;Krishna Modi&lt;/a&gt;,&lt;/em&gt;* which clearly shows the setup on Google’s side.&lt;/p&gt;

&lt;h2&gt;
  
  
  Federated access via Leapp CLI
&lt;/h2&gt;

&lt;p&gt;You can download Leapp CLI by following the instructions in our official &lt;a href="https://docs.leapp.cloud/0.13.1/installation/install-leapp/" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;💡 &lt;em&gt;Note that you also need the Leapp Desktop App in order to use the CLI&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After that, we can create a script that allows creating a new Federated Session:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="c"&gt;# Make a new Federated Session&lt;/span&gt;

&lt;span class="c"&gt;# Get Parameters&lt;/span&gt;
&lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="nb"&gt;getopts&lt;/span&gt; &lt;span class="s2"&gt;":s:r:a:u:n:"&lt;/span&gt; flag&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;do
    case&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;flag&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="k"&gt;in
        &lt;/span&gt;s&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;OPTARG&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;;&lt;/span&gt;
        r&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;awsregion&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;OPTARG&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;;&lt;/span&gt;
        a&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;idparn&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;OPTARG&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;;&lt;/span&gt;
        u&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;idpurl&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;OPTARG&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;;&lt;/span&gt;
        n&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;rolearn&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;OPTARG&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;;&lt;/span&gt;
    &lt;span class="k"&gt;esac&lt;/span&gt;
&lt;span class="k"&gt;done

&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$name&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$awsregion&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$idparn&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$idpurl&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$rolearn&lt;/span&gt;

&lt;span class="c"&gt;# Find Default Profile ID&lt;/span&gt;
&lt;span class="nv"&gt;profile&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;leapp profile list &lt;span class="nt"&gt;-x&lt;/span&gt; &lt;span class="nt"&gt;--filter&lt;/span&gt; &lt;span class="s2"&gt;"Profile Name"&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;default | &lt;span class="nb"&gt;grep &lt;/span&gt;default | &lt;span class="nb"&gt;cut&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s2"&gt;" "&lt;/span&gt; &lt;span class="nt"&gt;-f2&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$profile&lt;/span&gt;

&lt;span class="c"&gt;# Create Session&lt;/span&gt;
leapp session add &lt;span class="nt"&gt;--providerType&lt;/span&gt; aws &lt;span class="nt"&gt;--sessionType&lt;/span&gt; awsIamRoleFederated &lt;span class="nt"&gt;--sessionName&lt;/span&gt; &lt;span class="nv"&gt;$name&lt;/span&gt; &lt;span class="nt"&gt;--region&lt;/span&gt; &lt;span class="nv"&gt;$awsregion&lt;/span&gt; &lt;span class="nt"&gt;--idpArn&lt;/span&gt; &lt;span class="nv"&gt;$idparn&lt;/span&gt; &lt;span class="nt"&gt;--idpUrl&lt;/span&gt; &lt;span class="nv"&gt;$idpurl&lt;/span&gt; &lt;span class="nt"&gt;--profileId&lt;/span&gt; &lt;span class="nv"&gt;$profile&lt;/span&gt; &lt;span class="nt"&gt;--roleArn&lt;/span&gt; &lt;span class="nv"&gt;$rolearn&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can remove the echo command, which is there to show you that all the arguments are passed correctly.&lt;/p&gt;

&lt;p&gt;You can invoke the command like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./testScript.sh &lt;span class="nt"&gt;-s&lt;/span&gt; MYTEST &lt;span class="nt"&gt;-r&lt;/span&gt; eu-west-1 &lt;span class="nt"&gt;-a&lt;/span&gt; FAKEARN &lt;span class="nt"&gt;-u&lt;/span&gt; https://fakeurl.com &lt;span class="nt"&gt;-n&lt;/span&gt; FAKEROLEARN
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;💡 &lt;em&gt;Note: remember that all arguments are mandatory. You can also modify the script to give the session a different profile; here, we use the default one.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This is the output, taken directly from my console and my Leapp Desktop App, to show synchronization is working correctly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3xt3mk3reydeasq4jzh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3xt3mk3reydeasq4jzh.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: create a chained session from the Federated one
&lt;/h2&gt;

&lt;p&gt;Usually, Federation is used to Access a Landing Zone, but most of the time, you want to access another account (i.e. one of your clients to work on a project).&lt;/p&gt;

&lt;p&gt;Here I’ll show how to create a chained session from a federated one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="c"&gt;# Make a chained from a Federated session&lt;/span&gt;

&lt;span class="c"&gt;# Get Parameters&lt;/span&gt;
&lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="nb"&gt;getopts&lt;/span&gt; &lt;span class="s2"&gt;":s:r:n:p:"&lt;/span&gt; flag&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;do
    case&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;flag&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="k"&gt;in
        &lt;/span&gt;s&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;OPTARG&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;;&lt;/span&gt;
        r&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;awsregion&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;OPTARG&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;;&lt;/span&gt;
        n&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;rolearn&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;OPTARG&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;;&lt;/span&gt;
        p&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;parentname&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;OPTARG&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;;&lt;/span&gt;
    &lt;span class="k"&gt;esac&lt;/span&gt;
&lt;span class="k"&gt;done

&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$name&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$awsregion&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$rolearn&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$parentname&lt;/span&gt;

&lt;span class="c"&gt;# Find Parent Session ID&lt;/span&gt;
&lt;span class="nv"&gt;parentid&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;leapp session list &lt;span class="nt"&gt;-x&lt;/span&gt; &lt;span class="nt"&gt;--filter&lt;/span&gt; &lt;span class="s2"&gt;"Session Name"&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$parentname&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nv"&gt;$parentname&lt;/span&gt; | &lt;span class="nb"&gt;cut&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s2"&gt;" "&lt;/span&gt; &lt;span class="nt"&gt;-f2&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$parentid&lt;/span&gt;

&lt;span class="c"&gt;# Find Default Profile ID&lt;/span&gt;
&lt;span class="nv"&gt;profile&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;leapp profile list &lt;span class="nt"&gt;-x&lt;/span&gt; &lt;span class="nt"&gt;--filter&lt;/span&gt; &lt;span class="s2"&gt;"Profile Name"&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;default | &lt;span class="nb"&gt;grep &lt;/span&gt;default | &lt;span class="nb"&gt;cut&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s2"&gt;" "&lt;/span&gt; &lt;span class="nt"&gt;-f2&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$profile&lt;/span&gt;

&lt;span class="c"&gt;# Create Session&lt;/span&gt;
leapp session add &lt;span class="nt"&gt;--providerType&lt;/span&gt; aws &lt;span class="nt"&gt;--sessionType&lt;/span&gt; awsIamRoleChained &lt;span class="nt"&gt;--sessionName&lt;/span&gt; &lt;span class="nv"&gt;$name&lt;/span&gt; &lt;span class="nt"&gt;--region&lt;/span&gt; &lt;span class="nv"&gt;$awsregion&lt;/span&gt; &lt;span class="nt"&gt;--profileId&lt;/span&gt; &lt;span class="nv"&gt;$profile&lt;/span&gt; &lt;span class="nt"&gt;--roleArn&lt;/span&gt; &lt;span class="nv"&gt;$rolearn&lt;/span&gt; &lt;span class="nt"&gt;--parentSessionId&lt;/span&gt; &lt;span class="nv"&gt;$parentid&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is the result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxliyo79zgpzygfl8qu6d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxliyo79zgpzygfl8qu6d.png" alt="image"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;As you can see, the properties are correctly imported from the CLI:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbxrcc4e8ruttszvr758f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbxrcc4e8ruttszvr758f.png" alt="image"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this small tutorial I’ve shown how it is possible to create a Federated Session directly from your terminal using Leapp CLI! &lt;/p&gt;

&lt;p&gt;Also, we have seen that scripts leveraging the Leapp CLI can be combined together to achieve even more interesting results (i.e. creating a Chained Session from a Federated one).&lt;/p&gt;

&lt;p&gt;This is just the first of a series of small tutorials we, as the Noovolari team, will propose to you all to show how you can automate some of your credential processes using Leapp CLI.&lt;/p&gt;

&lt;p&gt;As always, if you have suggestions or questions, feel free to come and have a chat with us on our &lt;a href="https://join.slack.com/t/noovolari/shared_invite/zt-opn8q98k-HDZfpJ2_2U3RdTnN~u_B~Q" rel="noopener noreferrer"&gt;community slack&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Until next time, see ya and stay safe! 🙂&lt;/p&gt;

</description>
      <category>aws</category>
      <category>federatedaccess</category>
      <category>tutorial</category>
      <category>cli</category>
    </item>
    <item>
      <title>Stop putting AWS credentials in the credentials file</title>
      <dc:creator>Alessandro Gaggia</dc:creator>
      <pubDate>Fri, 27 May 2022 08:37:07 +0000</pubDate>
      <link>https://dev.to/aws-builders/stop-putting-aws-credentials-in-the-credentials-file-3f7e</link>
      <guid>https://dev.to/aws-builders/stop-putting-aws-credentials-in-the-credentials-file-3f7e</guid>
      <description>&lt;h2&gt;
  
  
  From Ben Kehoe thread to an OSS tool for the on-demand credentials generation
&lt;/h2&gt;

&lt;p&gt;To me, Everything started with this Twitter thread and from that &lt;a href="https://ben11kehoe.medium.com/never-put-aws-temporary-credentials-in-env-vars-or-credentials-files-theres-a-better-way-25ec45b4d73e" rel="noopener noreferrer"&gt;article&lt;/a&gt;, by Ben Kehoe:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj9xl1r6ohbg38a7dzvpi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj9xl1r6ohbg38a7dzvpi.png" alt="Image"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;When dealing with CLI or SDKs, we are already used to putting IAM credentials in the &lt;code&gt;.aws/credentials&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;However, as correctly stated cluttering the credentials file with many credentials is &lt;strong&gt;not&lt;/strong&gt; the right move to make.&lt;/p&gt;

&lt;p&gt;This article aims to overview what IAM Principals are and the better ways to manage your programmatic access to AWS.&lt;/p&gt;

&lt;p&gt;Moreover, we'll focus on the Credential Process. We'll see that we can polish our local usage of AWS Principals with it.&lt;/p&gt;

&lt;p&gt;Let's dig into this journey through AWS authorization systems.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's start with some basics: IAM Principals!
&lt;/h2&gt;

&lt;p&gt;Stating the official &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/intro-structure.html" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;: &lt;em&gt;"A principal is a person or application that can request an action or operation on an AWS account or resource."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;IAM Principals can either be the &lt;strong&gt;User Root Account&lt;/strong&gt; or an &lt;strong&gt;IAM entity&lt;/strong&gt; (User or Role).&lt;/p&gt;

&lt;p&gt;Because &lt;strong&gt;Root Account,&lt;/strong&gt; following &lt;a href="https://docs.aws.amazon.com/accounts/latest/reference/best-practices-root-user.html" rel="noopener noreferrer"&gt;best practices&lt;/a&gt;, &lt;strong&gt;should not be used for programmatic access,&lt;/strong&gt; and &lt;strong&gt;IAM Users&lt;/strong&gt; are not a dynamic way to manage AWS identities, the provider is pushing users to use &lt;strong&gt;IAM Roles.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;AWS Roles can be used to &lt;strong&gt;grant permissions to external Identity providers&lt;/strong&gt; (Okta, OneLogin, AzureAd, and more..) to assign them to SSO Identities.&lt;/p&gt;

&lt;p&gt;By using AWS Roles, an entity can also obtain permission in another existing AWS account via &lt;a href="https://aws.amazon.com/premiumsupport/knowledge-center/iam-assume-role-cli/?nc1=h_ls" rel="noopener noreferrer"&gt;the assumeRole technique&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  You obtained your Principal. Now what? Gain access to other Accounts!
&lt;/h2&gt;

&lt;p&gt;One of the best things about IAM Roles is that they can be "assumed" by letting other entities (Users, Services, Applications) act &lt;strong&gt;on behalf of that role&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here is a &lt;a href="https://medium.com/leapp-cloud/aws-sso-vs-cross-account-role-based-iam-access-why-and-how-to-use-roles-2521517d28e1" rel="noopener noreferrer"&gt;comprehensive article&lt;/a&gt; that explains how you can gain access to other AWS accounts!&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS SSO: it's an authentication process, not a Principal!
&lt;/h2&gt;

&lt;p&gt;Ok, we have seen how to use Principals in the most common scenarios.&lt;/p&gt;

&lt;p&gt;We also know that it is common for companies to divide their tenants or simply their workloads into different accounts to maintain isolation and simplify governance and management.&lt;/p&gt;

&lt;p&gt;In a modern AWS environment, AWS advice is to use &lt;a href="https://aws.amazon.com/it/organizations/" rel="noopener noreferrer"&gt;AWS Organizations&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq1uvq01tj278tktdnm5t.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq1uvq01tj278tktdnm5t.jpg" alt="Image"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Courtesy of Amazon Web Services&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;AWS Organizations allows for AWS Single Sign-On, which is the ability to authenticate a valid external Identity, into the AWS ecosystem, through a &lt;a href="https://aws.amazon.com/identity/federation/" rel="noopener noreferrer"&gt;Federation Process&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;One important thing to understand clearly is that &lt;strong&gt;AWS SSO is not a Principal!&lt;/strong&gt; AWS SSO allows for &lt;strong&gt;authentication&lt;/strong&gt; inside AWS by external meanings, but it &lt;strong&gt;does not handle authorization against AWS services&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;AWS associates &lt;a href="https://medium.com/leapp-cloud/aws-sso-vs-cross-account-role-based-iam-access-why-and-how-to-use-roles-2521517d28e1" rel="noopener noreferrer"&gt;one or more IAM Roles to an authenticated user via AWS SSO to manage authorization.&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Signing API requests to AWS
&lt;/h2&gt;

&lt;p&gt;You can put AWS credentials inside credentials or config files. But do you know how AWS effectively authorizes requesters made with those credentials? With a process called &lt;a href="https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html" rel="noopener noreferrer"&gt;Signature V4&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Access key id, secret access key, and session token are used to sign the HTTP requests you make to AWS services, verifying the principal accessing the service.&lt;/p&gt;

&lt;p&gt;They never get included in the request. Instead, you use it to create a cryptographic signature for the request.&lt;/p&gt;

&lt;p&gt;While SigV4 is embedded in all the AWS SDKs, it is not exposed to being used independently (e.g., &lt;a href="https://github.com/aws/aws-sdk-net/issues/958" rel="noopener noreferrer"&gt;API Gateway requires SigV4 signatures on requests&lt;/a&gt; that can't be made through the SDK).&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS Credentials file and temporary credentials
&lt;/h2&gt;

&lt;p&gt;A credentials file is a plain text file, located typically in the &lt;code&gt;~/.aws/&lt;/code&gt; folder. They can be long-lived (AWS IAM User) or short-lived (AWS IAM Role). &lt;/p&gt;

&lt;p&gt;The credentials format is the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;default]
aws_access_key_id &lt;span class="o"&gt;=&lt;/span&gt; &amp;lt;YOUR_ACCESS_KEY_ID&amp;gt;
aws_secret_access_key &lt;span class="o"&gt;=&lt;/span&gt;&amp;lt;YOUR_SECRET_ACCESS_KEY&amp;gt;
&lt;span class="nv"&gt;aws_session_token&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&amp;lt;AWS_SESSION_TOKEN&amp;gt; &lt;span class="c"&gt;#short-lived only&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;They are used directly (&lt;a href="https://medium.com/leapp-cloud/how-to-use-aws-named-profiles-86f3abe7547a" rel="noopener noreferrer"&gt;with a specific profile or with a default one&lt;/a&gt;) by AWS CLI and SDKs.&lt;/p&gt;

&lt;p&gt;They are directly associated with an IAM Principal.&lt;/p&gt;

&lt;p&gt;What are the caveats of such an approach? Let's describe some:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AWS SSO&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It can be a pain to manage as we now know that AWS SSO is not a principal, so it cannot be used directly and needs a way to obtain its role to generate temporary credentials.&lt;/p&gt;

&lt;p&gt;While this is not a problem per se, AWS CLI currently does not give an easy way to view which role is associated with a specific SSO user, and SDKs require custom code to obtain usable credentials.&lt;/p&gt;

&lt;p&gt;So, in general, AWS SSO still needs much manual intervention to be used effectively for programmatic access.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Short-lived credentials used in the credentials file can't be auto-rotated by AWS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Being set either manually or via third-party tools, temporary credentials inside the credentials file can't be rotated automatically. Ben Kehoe stated in his article that good practice should require the opposite.&lt;/p&gt;

&lt;p&gt;There are some particular scenarios in which this can cause serious difficulties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Long-running processes&lt;/strong&gt;: if you start a long-running third-party process, temporary credentials will expire along the way, generating exceptions in the task. It is a credential manager's responsibility to take care of the expiration.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Third-party tools for automatic rotation&lt;/strong&gt;: it is challenging to handle rotation without disrupting external processes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;General cluttering of credential files&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As correctly stated by Ben in his article, the credentials file usually becomes a list of long and short-lived credentials and different profiles.&lt;/p&gt;

&lt;p&gt;This list is not secure and challenging to manage.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is there a better way to manage all AWS access to services and accounts? Yes, it's called Credential Process
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.leapp.cloud/latest/security/credential-process/" rel="noopener noreferrer"&gt;The credential process&lt;/a&gt; handles AWS credentials to allow a third-party tool to generate credentials when requested by AWS CLI, APIs, or SDKs.&lt;/p&gt;

&lt;p&gt;By putting a named profile configuration set in the config file located in &lt;code&gt;~/.aws/config&lt;/code&gt; formatted like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;profile PROFILE_NAME]
&lt;span class="nv"&gt;credential_process&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;PROCESS_GENERATING_CREDENTIAL_PATH_SCRIPT
&lt;span class="nv"&gt;region&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;REGION
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The credential_process variable tells AWS how to request valid temporary credentials when needed.&lt;/p&gt;

&lt;p&gt;The credential process requires the external tool to return valid credentials in the format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"Version"&lt;/span&gt;: 1,
    &lt;span class="s2"&gt;"AccessKeyId"&lt;/span&gt;: &lt;span class="s2"&gt;"an AWS access key"&lt;/span&gt;,
    &lt;span class="s2"&gt;"SecretAccessKey"&lt;/span&gt;: &lt;span class="s2"&gt;"your AWS secret access key"&lt;/span&gt;,
    &lt;span class="s2"&gt;"SessionToken"&lt;/span&gt;: &lt;span class="s2"&gt;"the AWS session token for temporary credentials"&lt;/span&gt;,
    &lt;span class="s2"&gt;"Expiration"&lt;/span&gt;: &lt;span class="s2"&gt;"ISO8601 timestamp when the credentials expire"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Setting the expiration time variable allows AWS to know precisely when requesting the credentials again without disrupting the external process requiring AWS.&lt;/p&gt;

&lt;p&gt;Why is it better to use the credential process than the credentials file? &lt;strong&gt;Credentials auto-rotation!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One of the major drawbacks of using temporary credentials in the credentials file is that there is no simple and intelligent way to refresh expired credentials, not without compromising a long-running process or essential operations.&lt;/p&gt;

&lt;p&gt;There is another significant benefit: &lt;strong&gt;no credentials are written on your machine, in files, or environment variables&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Even if the credential process is the way to go, there are still some important factors to consider:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Credential Process credentials generator&lt;/strong&gt;: this implies that the tool you're using can handle diverse IAM Principals.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caching credentials:&lt;/strong&gt; because credentials generation is demanded by an external tool, caching credentials efficiently is also requested.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cluttering:&lt;/strong&gt; avoid a long list of config rules.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is where the OSS project &lt;a href="https://leapp.cloud/" rel="noopener noreferrer"&gt;Leapp&lt;/a&gt; can be your handiwork. Leapp can manage all major IAM Principals and AWS SSO, converting all of them into viable temporary credentials.&lt;/p&gt;

&lt;p&gt;Leapp is a valid credential process for AWS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;profile PROFILE_NAME]
&lt;span class="nv"&gt;credential_process&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;leapp session generate SESSION_ID
&lt;span class="nv"&gt;region&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;REGION
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In combination with the Desktop App, the config file itself can be managed by the App:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/2ZX9v3_NRvg"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusions
&lt;/h3&gt;

&lt;p&gt;In this article, we have come down a long journey through all the aspects of IAM Principals and how they are used to gain programmatic access to the plethora of services that AWS offers.&lt;/p&gt;

&lt;p&gt;We have seen how AWS SSO works with AWS Organizations and why it must not be mistaken for a Principal. But we have also seen all the advantages of AWS Organizations over regular cross-account access.&lt;/p&gt;

&lt;p&gt;Signature V4 was also explained, giving hints on how AWS authorizes requests coming from CLI and SDKs.&lt;/p&gt;

&lt;p&gt;We have understood the differences between credentials file and credentials process and why the latter is nowadays a much-preferred solution for long-running processes and strict security compliances.&lt;/p&gt;

&lt;p&gt;Finally, we have discussed Leapp, our open-source tool, showing how it can help deal with the credentials process.&lt;/p&gt;

&lt;p&gt;Have questions? Want to propose a solution? Need to request something particular? Drop a line on my Twitter or come and join us in our &lt;a href="https://join.slack.com/t/noovolari/shared_invite/zt-19143463b-qecVgLuq5IBQsOyG6kJ2Uw" rel="noopener noreferrer"&gt;slack&lt;/a&gt; community and share your thoughts.&lt;/p&gt;

&lt;p&gt;So that's all folks, till the next article, goodbye and stay safe 😷.&lt;/p&gt;

&lt;p&gt;*This article was written in pair with my co-worker &lt;a class="mentioned-user" href="https://dev.to/alessandro"&gt;@alessandro&lt;/a&gt; Gaggia (&lt;a class="mentioned-user" href="https://dev.to/balubor"&gt;@balubor&lt;/a&gt;)&lt;/p&gt;

</description>
      <category>aws</category>
      <category>iam</category>
      <category>security</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Shortcut for AWS CDK credentials: insanely simple setup for SSO, SAML, and named profiles</title>
      <dc:creator>Alessandro Gaggia</dc:creator>
      <pubDate>Thu, 12 May 2022 09:34:45 +0000</pubDate>
      <link>https://dev.to/aws-builders/shortcut-for-aws-cdk-credentials-insanely-simple-setup-for-sso-saml-and-named-profiles-35a9</link>
      <guid>https://dev.to/aws-builders/shortcut-for-aws-cdk-credentials-insanely-simple-setup-for-sso-saml-and-named-profiles-35a9</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;We all love CDK! Don’t we all? Since its introduction, we’ve finally got a Typescript tool to write IaC on AWS precisely and structured for serverless applications.&lt;/p&gt;

&lt;p&gt;But there is a crucial point that is not as agile as it could be: local credentials management.&lt;/p&gt;

&lt;p&gt;There is no simple way to re-use the same template in different environments without writing complex scripts and extensively using environment variables.&lt;/p&gt;

&lt;p&gt;Another point is that you are usually bound to long-term credentials most of the time, and that is a possible security threat.&lt;/p&gt;

&lt;p&gt;Lastly, according to the documentation, &lt;a href="https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html#getting_started_prerequisites" rel="noopener noreferrer"&gt;CDK partially supports Single Sign-on credentials&lt;/a&gt;. See &lt;a href="https://github.com/aws/aws-cdk/issues/5455" rel="noopener noreferrer"&gt;this&lt;/a&gt; issue.&lt;/p&gt;

&lt;p&gt;Not only for SSO but currently, there are still some open issues that can potentially be addressed using Leapp.&lt;/p&gt;

&lt;p&gt;For example, &lt;a href="https://github.com/aws/aws-cdk/issues/10867" rel="noopener noreferrer"&gt;this&lt;/a&gt; issue requests MFA caching. Currently, CDK prompts the user for MFA code every time a request is issued. Leapp can avoid this as it correctly caches credentials until the session token is expired.&lt;/p&gt;

&lt;p&gt;Furthermore, inconsistency issues like this &lt;a href="https://github.com/aws/aws-cdk/issues/13792" rel="noopener noreferrer"&gt;one&lt;/a&gt; are easily avoided as Leapp manages the credentials file for you.&lt;/p&gt;

&lt;p&gt;But fear not! This article will show you how you can improve your CDK templates by making wise use of our &lt;a href="https://github.com/Noovolari/leapp" rel="noopener noreferrer"&gt;open-source tool&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You’ll see that it is possible to automate credentials generation &lt;strong&gt;outside&lt;/strong&gt; the template &lt;em&gt;–&lt;/em&gt; keeping it drier and more straightforward &lt;em&gt;–&lt;/em&gt; use SSO and having more than one credential set active simultaneously. This reduces the possibility to deploy to the wrong environment to zero by using named profiles.&lt;/p&gt;

&lt;p&gt;We have prepared a simple test case that you can follow along to understand what are the possibilities and what you can do in your own project. Let’s begin!&lt;/p&gt;

&lt;h2&gt;
  
  
  It all starts with a simple example
&lt;/h2&gt;

&lt;p&gt;To better understand what advantages our tool can give to a developer, we want to show you snippets of CDK code and terminal commands without and &lt;strong&gt;with Leapp&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Our example consists of a CDK template to deploy the same S3 bucket in two different accounts. Even if very trivial, its purpose is to demonstrate how you can simplify your code by introducing Leapp into your developer routine.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwsj2ig6x0c8gy3t52snh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwsj2ig6x0c8gy3t52snh.png" alt="1"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Bootstrap
&lt;/h3&gt;

&lt;p&gt;When using CDK you &lt;strong&gt;first have to bootstrap the AWS environments&lt;/strong&gt; that you want to deploy your infrastructure in, if you haven’t already. Without Leapp, you would normally use this cdk bootstrap command, as suggested by &lt;a href="https://docs.aws.amazon.com/cdk/v2/guide/bootstrapping.html" rel="noopener noreferrer"&gt;official AWS documentation&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;cdk&lt;/span&gt; &lt;span class="nx"&gt;bootstrap&lt;/span&gt; &lt;span class="nx"&gt;aws&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//ACCOUNT-NUMBER-1/REGION-1 aws://ACCOUNT-NUMBER-2/REGION-2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we bootstrapped both the accounts you’ll need in a single command. The workload is error-prone, not safe, and can become very difficult to manage if you need to bootstrap lots of them.&lt;/p&gt;

&lt;p&gt;With Leapp, you need one or more sessions already set up (don’t know how to? Check it &lt;a href="https://docs.leapp.cloud/latest/configuration/" rel="noopener noreferrer"&gt;here&lt;/a&gt;!). Go to the desktop app, select your session and double click it.&lt;/p&gt;

&lt;p&gt;You can also change the region and named profile for that session. Left-click on it and select Change → Region/Named Profile&lt;/p&gt;

&lt;p&gt;Now you can type &lt;code&gt;cdk bootstrap&lt;/code&gt; and CDK will automatically bootstrap the session with the &lt;em&gt;default&lt;/em&gt; named profile in the region that you selected. When bootstrapping multiple accounts, or if you’re not using the default named profile, add the flag &lt;code&gt;cdk bootstrap --profile NAMED_PROFILE&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;If you don’t want to leave your terminal, you can use the newly released &lt;a href="https://docs.leapp.cloud/0.12.0/cli/" rel="noopener noreferrer"&gt;Leapp CLI&lt;/a&gt;! &lt;/p&gt;

&lt;p&gt;Use the command &lt;code&gt;leapp session start&lt;/code&gt; to select which session to start, change its region and named profile if you need to with &lt;code&gt;leapp session change-region&lt;/code&gt; and &lt;code&gt;leapp session change-profile&lt;/code&gt; and then you’re set!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4kniaaqk41izz8m2q6o9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4kniaaqk41izz8m2q6o9.png" alt="2"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Synth and Deploy
&lt;/h3&gt;

&lt;p&gt;First thing first! For our example to deploy properly, when you instantiate your CDK stack, make sure to set &lt;code&gt;env&lt;/code&gt; in your props to the following value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="na"&gt;account&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;***&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;***&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;CDK_DEFAULT_ACCOUNT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="na"&gt;region&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;***&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="o"&gt;***&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;CDK_DEFAULT_REGION&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi06c7yz441uz6it8grkc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi06c7yz441uz6it8grkc.png" alt="3"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;In a default CDK project created using &lt;code&gt;cdk init,&lt;/code&gt; you can find this file inside the &lt;strong&gt;bin folder&lt;/strong&gt;, and it’s also referenced in the &lt;code&gt;cdk.json&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Once everything’s ready and the accounts are properly bootstrapped, start your sessions in Leapp as you did for the bootstrap step. Remember to change region and named profile accordingly. If you want to reduce the possibility of error to the minimum, you can programmatically do that in a script using custom flags. Check the Bonus section!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cdk deploy&lt;/code&gt; will deploy in the default named profile session you set in Leapp.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cdk deploy --profile NAMED_PROFILE&lt;/code&gt; will deploy in a different named profile instead.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Single Sign-on with CDK
&lt;/h3&gt;

&lt;p&gt;As we said before, CDK only partially supports credentials generated by AWS Single Sign-on, BUT with Leapp it is possible to overcome this limitation. And without changing anything in your scripts too!&lt;/p&gt;

&lt;p&gt;You have to &lt;a href="https://docs.leapp.cloud/latest/configuring-integration/configure-aws-single-sign-on-integration/" rel="noopener noreferrer"&gt;create an *integration&lt;/a&gt; *****in Leapp,  like you would for an AWS session. See the video below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhpfblpu7daabjcekb5ts.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhpfblpu7daabjcekb5ts.gif" alt="4"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;By doing so, you’ll recover all your Organization accounts and roles; now you just have to start one of your SSO sessions and Leapp will create short-lived credentials &lt;strong&gt;completely compatible with CDK&lt;/strong&gt;! &lt;/p&gt;

&lt;p&gt;Boom! Now you’re using SSO with CDK without hassle!&lt;/p&gt;

&lt;h3&gt;
  
  
  Credential Process
&lt;/h3&gt;

&lt;p&gt;All the examples shown until now are based on short-lived credentials, which is awesome, but even if temporary, you are still leaving an open door to potential attackers: credentials in AWS files are still in plain text and therefore exploitable.&lt;/p&gt;

&lt;p&gt;To overcome this issue AWS also gives the ability to generate credentials on the fly, right before issuing an SDK or CLI command. This feature is called &lt;strong&gt;&lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-sourcing-external.html" rel="noopener noreferrer"&gt;credential process&lt;/a&gt;&lt;/strong&gt;!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;profile default]
credential_process &lt;span class="o"&gt;=&lt;/span&gt; leapp session generate SESSIONID
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Leapp overwrites the AWS config file command by adding the correct session ID for you and using its CLI to generate credentials in place of AWS.&lt;/p&gt;

&lt;p&gt;By doing this, &lt;strong&gt;every time CDK needs to access one or more SDK commands&lt;/strong&gt;, Leapp &lt;strong&gt;will automatically issue valid credentials, without writing anything in your files&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;And of course, it works with named profiles too!&lt;/p&gt;

&lt;h3&gt;
  
  
  Bonus: How to use Leapp CLI to automate CDK deploy
&lt;/h3&gt;

&lt;p&gt;Leapp comes with a CLI, which allows to automate all the actions you can do with the Desktop App via flags. &lt;/p&gt;

&lt;p&gt;In this simple snippet we want to show you how to create a named profile, associate it with a new &lt;a href="https://docs.leapp.cloud/0.11.1/configuring-session/configure-aws-iam-user/" rel="noopener noreferrer"&gt;AWS IAM User session&lt;/a&gt;, start that session and deploy your infrastructure, by setting a profile name beforehand.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;PROFILE_NAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;my-profile-name
&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; leapp profile create &lt;span class="nt"&gt;--profileName&lt;/span&gt; &lt;span class="nv"&gt;$PROFILE_NAME&lt;/span&gt; 
&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nv"&gt;PROFILE_ID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;leapp profile list &lt;span class="nt"&gt;-x&lt;/span&gt; | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nv"&gt;$PROFILE_NAME&lt;/span&gt; | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print $1}'&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt; 
&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; leapp session add 
    &lt;span class="nt"&gt;--providerType&lt;/span&gt; aws 
    &lt;span class="nt"&gt;--sessionType&lt;/span&gt; awsIamUser 
    &lt;span class="nt"&gt;--profileId&lt;/span&gt; &lt;span class="nv"&gt;$PROFILE_ID&lt;/span&gt; 
    &lt;span class="nt"&gt;--sessionName&lt;/span&gt; MY-SESSION-NAME 
    &lt;span class="nt"&gt;--region&lt;/span&gt; eu-west-1 
    &lt;span class="nt"&gt;--accessKey&lt;/span&gt; ACCESSKEY 
    &lt;span class="nt"&gt;--secretKey&lt;/span&gt; SECRETKEY 
&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nv"&gt;SESSION_ID&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;leapp session list &lt;span class="nt"&gt;-x&lt;/span&gt; | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s1"&gt;'MY-SESSION-NAME'&lt;/span&gt; | &lt;span class="nb"&gt;awk&lt;/span&gt; &lt;span class="s1"&gt;'{print $1}'&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt; 
&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; leapp session start &lt;span class="nt"&gt;--sessionId&lt;/span&gt; &lt;span class="nv"&gt;$SESSION_ID&lt;/span&gt; 
&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; cdk deploy &lt;span class="nt"&gt;--profile&lt;/span&gt; &lt;span class="nv"&gt;$PROFILE_NAME&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  To conclude
&lt;/h2&gt;

&lt;p&gt;In this article, we’ve seen how you can improve the security of your CDK templates by leveraging Leapp as your credential management system. &lt;/p&gt;

&lt;p&gt;By using Leapp, you don’t need to write any long or short-lived credential neither in your credential (or config) file nor environment variables. And by using the credential process feature you don’t need credentials at all!&lt;/p&gt;

&lt;p&gt;We have shown how CDK supports named profiles which is a feature also managed by Leapp, so you can keep all your credentials active simultaneously, reducing the context switch between your IDE and Leapp.&lt;/p&gt;

&lt;p&gt;We have showcased some scripts that let you integrate your CDK work routine with Leapp CLI to simplify your daily operations even further.&lt;/p&gt;

&lt;p&gt;Thanks to Leapp generating temporary credentials from AWS SSO sessions, we have seen that you are also indirectly enabling Single Sign-on.&lt;/p&gt;

&lt;p&gt;We hope that these slight improvements will make your day-by-day work easier.  So, what are you planning to do with CDK? Do you have any suggestions on how we can improve Leapp? Come say “hi” in our &lt;a href="https://join.slack.com/t/noovolari/shared_invite/zt-opn8q98k-HDZfpJ2_2U3RdTnN~u_B~Q" rel="noopener noreferrer"&gt;community&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;Until next time, goodbye and stay safe 😷&lt;/p&gt;

&lt;p&gt;Noovolari team.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>security</category>
      <category>opensource</category>
      <category>cdk</category>
    </item>
    <item>
      <title>10 best practices to secure your AWS environment</title>
      <dc:creator>Alessandro Gaggia</dc:creator>
      <pubDate>Thu, 10 Mar 2022 14:29:41 +0000</pubDate>
      <link>https://dev.to/aws-builders/10-best-practices-to-secure-your-aws-environment-1e3k</link>
      <guid>https://dev.to/aws-builders/10-best-practices-to-secure-your-aws-environment-1e3k</guid>
      <description>&lt;p&gt;You registered to AWS. You are eager to start using it.&lt;/p&gt;

&lt;p&gt;You want to solve the problem that drove you to Amazon Web Services in the first place.&lt;/p&gt;

&lt;p&gt;However, when starting to use a Cloud Environment for work, &lt;strong&gt;security must be addressed first&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You need to secure your AWS resources, especially your accounts, from possible attacks. In this article, I want to describe 10 &lt;strong&gt;best practices to follow&lt;/strong&gt; to help you put your mind at peace when working with AWS.&lt;/p&gt;

&lt;p&gt;Let’s start!&lt;/p&gt;

&lt;h2&gt;
  
  
  Create Strong, Secure Password
&lt;/h2&gt;

&lt;p&gt;Always use &lt;strong&gt;randomly-generated solid&lt;/strong&gt; passwords with at least &lt;strong&gt;24-30 characters, symbols, uppercase letters, and numbers.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Because these passwords are complex, always use a secure vault to store and retrieve them easily. Something like &lt;a href="https://www.lastpass.com/"&gt;LastPass&lt;/a&gt; or &lt;a href="https://bitwarden.com/"&gt;BitWarden&lt;/a&gt; can help! Give it a try.&lt;/p&gt;

&lt;h2&gt;
  
  
  Multi-Factor Authentication Everywhere
&lt;/h2&gt;

&lt;p&gt;AWS allows for Multi-Factor Authentication for the root account (&lt;strong&gt;indispensable&lt;/strong&gt;) and other accounts. Try to consistently enforce MFA whenever possible if you don’t want your account hacked, &lt;a href="https://www.reddit.com/r/aws/comments/rvi9a8/thanks_to_all_of_the_my_account_was_hacked_posts/"&gt;or else&lt;/a&gt;...&lt;/p&gt;

&lt;p&gt;To enable MFA for &lt;strong&gt;the root account&lt;/strong&gt;, follow this &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_mfa_enable_virtual.html#enable-virt-mfa-for-root"&gt;guide&lt;/a&gt;. To enforce MFA for IAM users, follow this &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_mfa_enable_virtual.html#enable-virt-mfa-for-iam-user"&gt;guide&lt;/a&gt; instead.&lt;/p&gt;

&lt;p&gt;If you are a developer and need to access programmatically, you can use &lt;a href="https://github.com/Noovolari/leapp"&gt;Leapp&lt;/a&gt; to access with MFA.&lt;/p&gt;

&lt;p&gt;Remember also that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can add more than one device (or virtual device) per account or user.&lt;/li&gt;
&lt;li&gt;You can &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_mfa_enable_virtual.html#replace-virt-mfa"&gt;replace or “rotate”&lt;/a&gt; a device.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Remove credentials from your root account
&lt;/h2&gt;

&lt;p&gt;Your root account must be used only once to register to AWS and must NOT be used for anything else, as it can access everything in your environment. One of the first step*&lt;em&gt;s&lt;/em&gt;&lt;em&gt; is removing &lt;/em&gt;&lt;em&gt;programmatic access from it&lt;/em&gt;*.&lt;/p&gt;

&lt;p&gt;Create an IAM user and grant that user only the permissions needed for the APIs you plan to call.&lt;/p&gt;

&lt;p&gt;Follow this &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#lock-away-credentials"&gt;guide&lt;/a&gt; to understand how to do it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enable CloudTrail logging, store trails into separated account
&lt;/h2&gt;

&lt;p&gt;Log everything that happens in your AWS environment for security breach detection and investigation with CloudTrail.&lt;/p&gt;

&lt;p&gt;The following &lt;strong&gt;best practices are general guidelines&lt;/strong&gt;: you can tweak them as you see fit!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Apply trails to all AWS Regions:&lt;/strong&gt; ensure that all events that occur in your AWS account are logged.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Enable CloudTrail log file integrity:&lt;/strong&gt; lets you know if a log file has been deleted or changed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Always store the logs in a separate account&lt;/strong&gt;: enforce strict security controls, access, and segregation of duties.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use AWS KMS managed keys instead of standard S3-SSE: t&lt;/strong&gt;o provide a directly manageable security layer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Get started with this &lt;a href="https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-tutorial.html"&gt;guide&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Operate with IAM users, groups, and roles
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Don’t use the root user for your everyday routine.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead, citing AWS: &lt;em&gt;adhere to the &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#create-iam-users"&gt;best practice of using the root user only to create your first IAM user&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Then you can safely store away access and secret credentials. You’ll generally not be needing them unless there are a few specific &lt;a href="https://docs.aws.amazon.com/general/latest/gr/root-vs-iam.html#aws_tasks-that-require-root"&gt;tasks&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IAM users&lt;/strong&gt; represent an IAM identity capable of doing operations on AWS resources. &lt;strong&gt;User Groups&lt;/strong&gt; are collections of IAM users seen as a unit. Finally, a role is attached directly to a resource and allows it to perform operations on other resources.&lt;/p&gt;

&lt;p&gt;By exploiting these instruments, you can isolate and manage who or what is doing something in your environment in a more structured way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Apply the Least Privilege Principle on IAM policies
&lt;/h2&gt;

&lt;p&gt;Avoid policies with Administrator access for everything!&lt;/p&gt;

&lt;p&gt;Start with a &lt;strong&gt;Deny All permission&lt;/strong&gt; and slowly add permissions for specific services you need for the task: this way, you’ll adhere to the Least Privilege principle.&lt;/p&gt;

&lt;p&gt;This is a &lt;a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege"&gt;small list of tricks&lt;/a&gt; that you can use to define the perfect policy for your service!&lt;/p&gt;

&lt;p&gt;Also, there is &lt;a href="https://github.com/iann0036/iamlive"&gt;an open-source tool&lt;/a&gt; you can use to define your policy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use AWS Organizations to set up your project accounts
&lt;/h2&gt;

&lt;p&gt;AWS Organizations is an account management service that enables the central management of multiple accounts. You can define your organizational structure to better sort and scope account usage with Organizational Units.&lt;/p&gt;

&lt;p&gt;Also, AWS Organizations include consolidated billing to see the overall breakdown of expenses. This helps you find anomalies quicker.&lt;/p&gt;

&lt;p&gt;If you have AWS Single Sign-On enabled in your Organization, you can access programmatically to your &lt;a href="https://docs.leapp.cloud/0.9.0/configuring-integration/configure-aws-single-sign-on-integration/"&gt;eligible roles through Leapp&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;AWS Organization uses &lt;strong&gt;&lt;a href="https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_scps.html"&gt;Service Control Policies&lt;/a&gt;&lt;/strong&gt; for security purposes, which act on top of IAM Policies across all accounts, limiting their maximum available permissions. Applied to Organizational Units help define permission boundaries in different company areas.&lt;/p&gt;

&lt;p&gt;Follow this guide to help you correctly set up your landing zone and your project’s accounts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enable AWS Config rules and Billing Alarms
&lt;/h2&gt;

&lt;p&gt;AWS has instruments to &lt;strong&gt;verify tampering or unwanted use of AWS Resources&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;AWS Config provides a detailed view of AWS resources’ configurations in your account. This includes how they are related to each other and how they were configured in the past. AWS Config allows us to understand how they have changed over time.&lt;/p&gt;

&lt;p&gt;Setting up AWS Config is an excellent way for &lt;strong&gt;proactive and detective&lt;/strong&gt; actions. Use this &lt;a href="https://docs.aws.amazon.com/config/latest/developerguide/getting-started.html"&gt;guide&lt;/a&gt; to configure it and remember to &lt;strong&gt;enable AWS Config in all accounts and Regions.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is an industry best practice recommended by the &lt;a href="https://aws.amazon.com/blogs/security/announcing-industry-best-practices-for-securing-aws-resources/"&gt;Center for Internet Security&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Billing alarms and Billing thresholds&lt;/strong&gt; are another great way to be notified if something is not right in your account. Configure them like this &lt;a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/monitor_estimated_charges_with_cloudwatch.html"&gt;guide&lt;/a&gt;, remembering to &lt;strong&gt;apply clean and meaningful TAGs&lt;/strong&gt; to all resources.&lt;/p&gt;

&lt;h2&gt;
  
  
  Apply security at all layers
&lt;/h2&gt;

&lt;p&gt;When building a project with AWS, always strive to secure all layers of your application and your environment:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Apply HTTPS as protocol; you can do it with services like API Gateway, CloudFront, LoadBalancers, even with plain EC2 instances; also remember &lt;a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/rest-api-mutual-tls.html"&gt;mutual TLS authentication&lt;/a&gt; if you are developing a B2B application.&lt;/li&gt;
&lt;li&gt;Apply security groups to your resources to manage carefully what CIDR, specific IP addresses, and Ports can communicate in and out of your environment.&lt;/li&gt;
&lt;li&gt;Try to apply a good network design, isolating all the resources that don’t need direct Internet access in private VPCs, thus reducing ingress to selected Gateways that are easy to monitor.&lt;/li&gt;
&lt;li&gt;Always apply &lt;strong&gt;encryption in transit and at rest whenever possible.&lt;/strong&gt; Remember that AWS offers direct encryption in S3, KMS for key management, and the ability to encrypt EBS volumes directly.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Use temporary generated credentials
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Credentials and .aws folder are possible vectors of malicious attacks.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To secure all your credentials and remove the hassle of creating temporary credentials via IAM Simple Token Service, I would suggest the &lt;strong&gt;open-source project &lt;a href="https://www.leapp.cloud/"&gt;Leapp&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Some of its features at a glance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Cloud credentials generation in 1 click&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data &lt;a href="https://docs.leapp.cloud/latest/security/system-vault/"&gt;stored locally encrypted&lt;/a&gt; in the OS System Vault&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multiple Cloud-Access supported &lt;a href="https://docs.leapp.cloud/latest/configuration/"&gt;strategies&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Automatic &lt;a href="https://docs.leapp.cloud/latest/security/credentials-generation/aws/"&gt;short-lived credentials rotation&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Automated provisioning of &lt;a href="https://docs.leapp.cloud/latest/sessions/"&gt;Sessions&lt;/a&gt; from &lt;a href="https://docs.leapp.cloud/latest/configuring-integration/configure-aws-single-sign-on-integration/"&gt;AWS Single Sign-on&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;A friendly and slick user interface 🙂&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;With all these tips and tricks, I hope to have shown you how you can enhance the security posture of your AWS Cloud Environment.&lt;/p&gt;

&lt;p&gt;Until next time, stay safe, and thanks for reading.&lt;/p&gt;

&lt;p&gt;Noovolari team.&lt;/p&gt;

&lt;p&gt;Until next time, see you all 🙂.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>iam</category>
      <category>security</category>
      <category>opensource</category>
    </item>
    <item>
      <title>AWS SDK: "Unable to locate credentials", a cheat sheet for solving the issue</title>
      <dc:creator>Alessandro Gaggia</dc:creator>
      <pubDate>Thu, 21 Oct 2021 14:09:14 +0000</pubDate>
      <link>https://dev.to/aws-builders/aws-sdk-unable-to-locate-credentials-a-cheat-sheet-for-solving-the-issue-5ed6</link>
      <guid>https://dev.to/aws-builders/aws-sdk-unable-to-locate-credentials-a-cheat-sheet-for-solving-the-issue-5ed6</guid>
      <description>&lt;p&gt;How often do you find yourself working with your carefully prepared code, with AWS SDK in place for calling all of your services, and...nothing, it seems that your credentials are missing, even if you have configured them correctly?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F1236%2F1%2A_q-rIszA7p97_fDrZi_jVA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F1236%2F1%2A_q-rIszA7p97_fDrZi_jVA.jpeg" alt="https://miro.medium.com/max/1236/1*_q-rIszA7p97_fDrZi_jVA.jpeg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you've incurred this problem at least once, you're in the right place! We'll see together how to mangle with typical use cases, with an eye on security concerns.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Credentials provider chain
&lt;/h2&gt;

&lt;p&gt;First things first! To understand why you'll receive these errors, you must know how AWS searches for valid credentials.&lt;/p&gt;

&lt;p&gt;That is the &lt;strong&gt;credentials provider chain&lt;/strong&gt;! Some locations in your computer can hold credentials, &lt;a href="https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html" rel="noopener noreferrer"&gt;as stated by AWS&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;When you initialize from the SDK a new service client without supplying credentials directly (supply credentials directly in code is a &lt;strong&gt;bad practice&lt;/strong&gt;, by the way), the SDK attempts to find AWS credentials by searching them in this order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Environment variables:&lt;/strong&gt; &lt;code&gt;AWS_ACCESS_KEY_ID&lt;/code&gt; and &lt;code&gt;AWS_SECRET_ACCESS_KEY&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Web Identity Token credentials:&lt;/strong&gt; from the environment or container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The default credential profiles file:&lt;/strong&gt; &lt;code&gt;~/.aws/credentials&lt;/code&gt;, which is the &lt;strong&gt;most common location,&lt;/strong&gt; or the &lt;strong&gt;config file:&lt;/strong&gt; &lt;code&gt;~/.aws/config&lt;/code&gt;, generated with the &lt;strong&gt;CLI command&lt;/strong&gt; &lt;code&gt;aws configure&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Amazon ECS container credentials:&lt;/strong&gt; from the Amazon ECS if the environment variable &lt;code&gt;AWS_CONTAINER_CREDENTIALS_RELATIVE_URI&lt;/code&gt; is set.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Instance profile credentials:&lt;/strong&gt; used on EC2 instances and delivered through the Amazon EC2 metadata service.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If credentials are not set at least in one of the places or overlapping in a way that is not intended, then an error is likely to occur. Let's begin with some well-known use cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unable to locate credentials: install the AWS CLI and run an aws configure!
&lt;/h2&gt;

&lt;p&gt;It is &lt;strong&gt;always better to install AWS CLI&lt;/strong&gt;, even if you don't plan to use it; you'll see that there are plenty of advantages to installing it. For example, once installed, it is possible to run &lt;code&gt;aws configure&lt;/code&gt;, which will set all the required files for you to start using environment credentials and stop using hardcoded ones.&lt;/p&gt;

&lt;p&gt;Let's see how it can be compiled:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F1388%2F1%2AVkNICF0hdMKY9tyzyh8uQw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F1388%2F1%2AVkNICF0hdMKY9tyzyh8uQw.png" alt="https://miro.medium.com/max/1388/1*VkNICF0hdMKY9tyzyh8uQw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Missing credentials in config: when code order matters!
&lt;/h2&gt;

&lt;p&gt;Let's take for example the Javascript SDK, but the same case applies to all AWS SDK: if you have changed your credentials after some operations your Javascript client will not read the updated credentials unless you call an explicit &lt;strong&gt;config update&lt;/strong&gt;, just like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;AWS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loadFromPath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./AwsConfig.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// OR for example in case of temporary new credentials&lt;/span&gt;
&lt;span class="nx"&gt;AWS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;sessionToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;sessionToken&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;aws_session_token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;accessKeyId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;sessionToken&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;aws_access_key_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;secretAccessKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;sessionToken&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;aws_secret_access_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="c1"&gt;// THEN for example&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;s3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;AWS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;S3&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;So it's important to call an explicit update of the AWS library before instantiating a client.&lt;/p&gt;
&lt;h2&gt;
  
  
  Temporary credentials generated programmatically, out of synch!
&lt;/h2&gt;

&lt;p&gt;It's rare, but it may happen: sometimes, depending on the SDK version, especially for Javascript, for some services, the credentials are not working as expected and may seem out of synch with the AWS service.&lt;/p&gt;

&lt;p&gt;In this case, there could be two potential fixes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;try to update the library. It is not so uncommon to see the error disappearing after an SDK update;&lt;/li&gt;
&lt;li&gt;Your clock could be out of sync. Check if, for some reason, it is not showing the right time.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Check environment variables when running terminal commands from code
&lt;/h2&gt;

&lt;p&gt;Sometimes using libraries or tools for launching terminal commands and scripts can be a huge help. Let's consider Node's &lt;code&gt;require('child_process').exec&lt;/code&gt;, a very interesting method for spawning child processes.&lt;/p&gt;

&lt;p&gt;Still, when used in a &lt;strong&gt;sandboxed application&lt;/strong&gt; (i.e., Electron ones), it runs &lt;strong&gt;without&lt;/strong&gt; your user's &lt;strong&gt;environment variables&lt;/strong&gt;, thus making your credentials useless unless you set them explicitly in the environment option like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;env&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;AWS_ACCESS_KEY_ID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;aws_access_key_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;AWS_SECRET_ACCESS_KEY&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;aws_secret_access_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;AWS_SESSION_TOKEN&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;aws_session_token&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;(&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;command&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;, &lt;span class="si"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;env&lt;/span&gt; &lt;span class="si"&gt;}&lt;/span&gt;);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Even if this is a Node/Typescript example, the same principle still applies for Python, Rust, Go, and so on. Check if the &lt;strong&gt;command is issued with the proper environment set&lt;/strong&gt;, it will save you a lot of time and trouble!&lt;/p&gt;
&lt;h2&gt;
  
  
  Privileged access comes with a price: watch out for sudo!
&lt;/h2&gt;

&lt;p&gt;This particular error is nasty as you typically have your proper credentials in place.&lt;/p&gt;

&lt;p&gt;That is, you run either a CLI command or invoke a terminal command (or script) from code, and credentials are missing!&lt;/p&gt;

&lt;p&gt;This happens because &lt;strong&gt;sudo changes your&lt;/strong&gt; &lt;code&gt;$HOME&lt;/code&gt; directory from &lt;code&gt;~/USER&lt;/code&gt; to &lt;code&gt;/root&lt;/code&gt;, removing most bash variables like &lt;code&gt;AWS_CONFIG_FILE&lt;/code&gt; from the environment. Make sure you do everything with AWS as root or as your user, don't mix.&lt;/p&gt;
&lt;h2&gt;
  
  
  Sidenotes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;If you have the CLI installed: try running &lt;code&gt;aws configure list&lt;/code&gt; to see what credentials are set&lt;/li&gt;
&lt;li&gt;If you are uncertain about a particular credentials error, run the command you want by CLI using the &lt;code&gt;--debug&lt;/code&gt; option.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;Managing all credentials by hand and constantly keeping an eye on what is active and what is not can be very troublesome.&lt;/p&gt;

&lt;p&gt;What about security? &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is highly inconvenient to use &lt;strong&gt;long-lived credentials&lt;/strong&gt; (i.e., IAM user) in both the environment variables and the credentials file, let alone forget to remove them once they are unnecessary.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Leaving credentials exposed like this allows for potential attacks on your AWS account or that of your client. You want to increase security is to use &lt;strong&gt;short-lived credentials&lt;/strong&gt; whenever is possible to limit an attacker's blast radius (they can be used for an hour at maximum).&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/Noovolari" rel="noopener noreferrer"&gt;
        Noovolari
      &lt;/a&gt; / &lt;a href="https://github.com/Noovolari/leapp" rel="noopener noreferrer"&gt;
        leapp
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Leapp is the DevTool to access your cloud
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;p&gt;
  &lt;a rel="noopener noreferrer" href="https://github.com/Noovolari/leapp.github/images/README-1.png#gh-dark-mode-only"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2FNoovolari%2Fleapp.github%2Fimages%2FREADME-1.png%23gh-dark-mode-only" alt="Leapp" height="150"&gt;&lt;/a&gt;
    &lt;a rel="noopener noreferrer" href="https://github.com/Noovolari/leapp.github/images/README-1-dark.png#gh-light-mode-only"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2FNoovolari%2Fleapp.github%2Fimages%2FREADME-1-dark.png%23gh-light-mode-only" alt="Leapp" height="150"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Leapp&lt;/h1&gt;
&lt;/div&gt;

&lt;div class="markdown-heading"&gt;
&lt;h4 class="heading-element"&gt;
  &lt;a href="https://www.leapp.cloud" rel="nofollow noopener noreferrer"&gt;Website&lt;/a&gt; |
  &lt;a href="https://roadmap.leapp.cloud/tabs/4-in-progress" rel="nofollow noopener noreferrer"&gt;Roadmap&lt;/a&gt; |
  &lt;a href="https://medium.com/leapp-cloud" rel="nofollow noopener noreferrer"&gt;Blog&lt;/a&gt; |
  &lt;a href="https://join.slack.com/t/noovolari/shared_invite/zt-opn8q98k-HDZfpJ2_2U3RdTnN~u_B~Q" rel="nofollow noopener noreferrer"&gt;TOPS community&lt;/a&gt; |
  &lt;a href="https://docs.leapp.cloud" rel="nofollow noopener noreferrer"&gt;Documentation&lt;/a&gt; |
  &lt;a href="https://docs.leapp.cloud/latest/troubleshooting/app-data/" rel="nofollow noopener noreferrer"&gt;Troubleshooting&lt;/a&gt;
&lt;/h4&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;a href="https://github.com/Noovolari/leapp/blob/master/LICENSE" rel="noopener noreferrer"&gt;&lt;img alt="License" src="https://camo.githubusercontent.com/17dbabd356010721fd158a5cb844cfe0d41314e02926fb4ad7fd088488cbdcc0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6e6f6f766f6c6172692f6c65617070"&gt;&lt;/a&gt;
  &lt;a href="https://join.slack.com/t/noovolari/shared_invite/zt-opn8q98k-HDZfpJ2_2U3RdTnN~u_B~Q" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/cdb9967c52736ff057d5000dd749a3ac0f738c01afb41eb74bd46815dd5c2ead/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f736c61636b2d6f6e6c696e652d677265656e" alt="Slack"&gt;&lt;/a&gt;
  &lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/dea373660641792617e07cc71f7d93ca86e4b394f8cbb11aebd5f68186dcda4e/68747470733a2f2f64336f35396173613875646371392e636c6f756466726f6e742e6e65742f636f7665726167652d6261646765732f636f72652d6261646765732e737667"&gt;&lt;img src="https://camo.githubusercontent.com/dea373660641792617e07cc71f7d93ca86e4b394f8cbb11aebd5f68186dcda4e/68747470733a2f2f64336f35396173613875646371392e636c6f756466726f6e742e6e65742f636f7665726167652d6261646765732f636f72652d6261646765732e737667"&gt;&lt;/a&gt;
  &lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/6c3b56aa94fd3131166acbb45f1fa91f4e04c60e7f11322636e43f1470df7927/68747470733a2f2f64336f35396173613875646371392e636c6f756466726f6e742e6e65742f636f7665726167652d6261646765732f636c692d6261646765732e737667"&gt;&lt;img src="https://camo.githubusercontent.com/6c3b56aa94fd3131166acbb45f1fa91f4e04c60e7f11322636e43f1470df7927/68747470733a2f2f64336f35396173613875646371392e636c6f756466726f6e742e6e65742f636f7665726167652d6261646765732f636c692d6261646765732e737667"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;⚡ Lightning Fast, Safe, Desktop App for Cloud credentials managing and generation&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Leapp&lt;/strong&gt; is a Cross-Platform Cloud access App, built on top of &lt;a href="https://github.com/electron/electron" rel="noopener noreferrer"&gt;Electron&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The App is designed to &lt;strong&gt;manage and secure Cloud Access in multi-account environments,&lt;/strong&gt; and it is available for MacOS, Windows, and Linux.&lt;/p&gt;

&lt;p&gt;For more information about features go to &lt;a href="https://docs.leapp.cloud/" rel="nofollow noopener noreferrer"&gt;our documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;
  &lt;a rel="noopener noreferrer" href="https://github.com/Noovolari/leapp.github/images/Leapp-animation.gif"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2FNoovolari%2Fleapp.github%2Fimages%2FLeapp-animation.gif" alt="Web interface gif"&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;✨ Features&lt;/h1&gt;
&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Cloud credentials generation in 1 click&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data &lt;a href="https://docs.leapp.cloud/latest/security/system-vault/" rel="nofollow noopener noreferrer"&gt;stored locally encrypted&lt;/a&gt; in the OS System Vault&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multiple Cloud-Access supported &lt;a href="https://docs.leapp.cloud/latest/configuration/" rel="nofollow noopener noreferrer"&gt;strategies&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Automatic &lt;a href="https://docs.leapp.cloud/latest/security/credentials-generation/aws/" rel="nofollow noopener noreferrer"&gt;short-lived credentials rotation&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Automatic provisioning of &lt;a href="https://docs.leapp.cloud/latest/sessions/" rel="nofollow noopener noreferrer"&gt;Sessions&lt;/a&gt; from &lt;a href="https://docs.leapp.cloud/latest/configuring-integration/configure-aws-single-sign-on-integration/" rel="nofollow noopener noreferrer"&gt;AWS Single Sign-on&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Open multiple AWS console from different AWS accounts in &lt;a href="https://addons.mozilla.org/it/firefox/addon/leapp-multi-console-extension/" rel="nofollow noopener noreferrer"&gt;Firefox&lt;/a&gt; and &lt;a href="https://docs.leapp.cloud/0.16.2/built-in-features/multi-console/#chrome-edge-and-other-chromium-based-browsers" rel="nofollow noopener noreferrer"&gt;Chrome&lt;/a&gt; web extensions!&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Connect to EC2 instances straight away&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Managing Leapp with its &lt;a href="https://docs.leapp.cloud/latest/cli/" rel="nofollow noopener noreferrer"&gt;CLI&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://docs.leapp.cloud/0.14.1/plugins/plugins-introduction/" rel="nofollow noopener noreferrer"&gt;Create your own Leapp plugin&lt;/a&gt;&lt;/strong&gt; to customize the App functionalities from the &lt;a href="https://github.com/Noovolari/leapp-plugin-template" rel="noopener noreferrer"&gt;template&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All the covered access methods can be found &lt;a href="https://docs.leapp.cloud/latest/configuration/" rel="nofollow noopener noreferrer"&gt;here&lt;/a&gt;…&lt;/p&gt;
&lt;/div&gt;


&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/Noovolari/leapp" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;p&gt;&lt;a href="https://leapp.cloud/" rel="noopener noreferrer"&gt;Leapp&lt;/a&gt; is an Open-Source tool that aims to manage the credentials provider chain for you.&lt;/p&gt;

&lt;p&gt;It uses short-lived credentials instead of hard-coded ones; it also removes them from the chain when not needed anymore.&lt;/p&gt;

&lt;p&gt;To sum up, we have seen different use cases to solve missing credentials errors when using AWS SDK or CLI commands. If you have any questions, by any means, feel free to &lt;a href="https://twitter.com/a_cava94" rel="noopener noreferrer"&gt;contact me&lt;/a&gt; or &lt;a href="https://join.slack.com/t/noovolari/shared_invite/zt-opn8q98k-HDZfpJ2_2U3RdTnN~u_B~Q" rel="noopener noreferrer"&gt;join our slack community&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Until next time, see you all 🙂.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>iam</category>
      <category>security</category>
      <category>howto</category>
    </item>
    <item>
      <title>Testing your Signature v4 Cognito authenticated API: an Insomnia Plugin</title>
      <dc:creator>Alessandro Gaggia</dc:creator>
      <pubDate>Tue, 03 Nov 2020 14:47:40 +0000</pubDate>
      <link>https://dev.to/urz9999/testing-your-signature-v4-cognito-authenticated-api-an-insomnia-plugin-3ocj</link>
      <guid>https://dev.to/urz9999/testing-your-signature-v4-cognito-authenticated-api-an-insomnia-plugin-3ocj</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Nowadays, applications are made more and more with a serverless approach in mind. Developers strive to separate as much as possible front-end tasks and structures from back-end and vice-versa.&lt;/p&gt;

&lt;p&gt;With this approach, the testing phase still remains extremely important, but sometimes due to several reasons, it becomes inconvenient for front-end and back-end teams to rely on each other for retrieving information useful for asserting their code.&lt;/p&gt;

&lt;p&gt;There are several reasons for that: time constraints, different speed in development, working on several branches, etc. Also, in our case, two extra motivations contribute to making this task more tedious than what it should have been.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;As said, we were transitioning to a Serverless approach, so no more View and Logic tied together in the same codebase.&lt;/li&gt;
&lt;li&gt;We used and still use Cognito as a means of authenticating our user pools for different applications, and many APIs are protected and need user's authentication for that.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;AWS&lt;/strong&gt; &lt;strong&gt;Cognito&lt;/strong&gt; is handy when it comes to creating authentication flows both for users and for APIs (especially with API Gateway), nonetheless without installing a library, &lt;strong&gt;mocking authentication with Signature V4 is no easy&lt;/strong&gt; task for sure.&lt;/p&gt;

&lt;p&gt;In the early stages of Leapp development, we, as a team, faced the same situation.&lt;/p&gt;

&lt;p&gt;Part of a side-project involving a Serverless architecture, filled with AWS Lambda functions as the back-end, required constant testing, being its logic complex and touching several areas of the software.&lt;/p&gt;

&lt;p&gt;Of course, this meant testing authenticated calls!&lt;/p&gt;

&lt;p&gt;We &lt;strong&gt;needed access to the API&lt;/strong&gt; (made with Lambdas) &lt;strong&gt;through API Gateway&lt;/strong&gt; to make different integration tests: this would make the REST ajax API from a mocked front-end using Signature V4 for authenticating the calls themselves.&lt;/p&gt;

&lt;p&gt;Usually, this is done quickly by regular Javascript/Typescript calls given you are using the suggested AWS SDK, but how you can do that when you need to mock your front-end in back-end tests besides calling it directly? We got a solution by exploiting &lt;a href="https://insomnia.rest/"&gt;Insomnia&lt;/a&gt; plugin feature and developing a system to integrate Cognito Authentication to Insomnia to allow back-end developers to test their logic by making API calls directly from the tool.&lt;/p&gt;

&lt;p&gt;This enhancement allowed us to decouple our testing phases and reduce the urge to keep the two teams always aligned. This is especially important because, as stated before, usually back-end can develop faster than the front-end as it doesn't have to deal with UI composing.&lt;/p&gt;

&lt;p&gt;Insomnia is a free cross-platform desktop application for making REST and GraphQL interacting with HTTP-based APIs. Insomnia combines an easy-to-use interface with advanced functionality like authentication helpers, code generation, and environment variables. It allows team collaboration, and it's a tool of choice by many big companies, including Netflix and Cisco.&lt;/p&gt;

&lt;p&gt;Moreover, Insomnia is an Open-Source project, acquired by &lt;a href="https://konghq.com/"&gt;Kong&lt;/a&gt;, and this was a motivator for us in choosing it because of the same very nature that drives our application &lt;a href="https://www.leapp.cloud/"&gt;Leapp&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;By using our integration, it is possible to do authenticated calls to APIs behind Amazon API Gateway with an authenticator enabled.&lt;/p&gt;

&lt;h2&gt;
  
  
  Plugin structure
&lt;/h2&gt;

&lt;p&gt;Insomnia gives some pretty neat instructions on how they want a plugin project to be structured.&lt;/p&gt;

&lt;p&gt;By following the reference on this page: &lt;a href="https://support.insomnia.rest/article/26-plugins"&gt;https://support.insomnia.rest/article/26-plugins&lt;/a&gt;, we have created a &lt;strong&gt;new node.js project.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We took extra attention to having a &lt;strong&gt;package.json&lt;/strong&gt; similar to the one provided below, and that points the key "main" to "./src/plugin.js" and have "insomnia" as one of the keys (this last part is essential to make Insomnia recognize the code as a plugin):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"insomnia-plugin-aws-cognito-token"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"0.11.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Plugin for Insomnia to provide Cognito JWT token from AWS"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./src/plugin.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"echo &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Error: no test specified&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; &amp;amp;&amp;amp; exit 1"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Leapp Team"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"license"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"MIT"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"repository"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"git"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"https://github.com/Noovolari/insomnia-plugin-aws-cognito-token.git"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"keywords"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"insomnia"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"plugin"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"AWS"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"Cognito"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"AppSync"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"insomnia"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"awscognitotoken"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Plugin for Insomnia to provide Cognito JWT token from AWS"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"amazon-cognito-identity-js"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^3.0.5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"aws-sdk"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^2.373.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"crypto-js"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^3.1.9-1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"jwt-decode"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^2.2.0"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then create a directory &lt;strong&gt;src&lt;/strong&gt; in the &lt;strong&gt;root&lt;/strong&gt; folder and add a new &lt;strong&gt;plugin.js file&lt;/strong&gt;. Inside the file we want a structure similar to this one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * Example template tag that generates a random number 
 * between a user-provided MIN and MAX
 */&lt;/span&gt;
&lt;span class="kr"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;templateTags&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;random&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;displayName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Random Integer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Generate random things&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;displayName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Minimum&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Minimum potential value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;defaultValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt; 
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;displayName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Maximum&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Maximum potential value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;defaultValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;min&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;min&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;max&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;min&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Template tags in the "args" section are used to define input variables, and "async run" uses positional variables to get them and run some logic for the plugin. This is, of course, a simplification, and you can undoubtedly call another method external to the &lt;strong&gt;module exports&lt;/strong&gt; to keep the code more maintainable, extensible, and clean.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to install the Insomnia plugin
&lt;/h2&gt;

&lt;p&gt;Installing plugins in Insomnia is so easy. You just have to go on the Preferences and select the plugin tab. Then type &lt;code&gt;insomnia-plugin-aws-cognito-token&lt;/code&gt; in the field that shows up and select "Install plugin." You have correctly installed the plugin!&lt;/p&gt;

&lt;h2&gt;
  
  
  Plugin Setup
&lt;/h2&gt;

&lt;p&gt;To make it works to your needs, go into Insomnia → Preferences: click on &lt;strong&gt;development&lt;/strong&gt; in the top-left corner, and choose "&lt;strong&gt;Manage Environments&lt;/strong&gt;"; a new window will open up to show your current environments. Following directives from the figure below, click in the right corner as shown to create a new &lt;strong&gt;JSON structure&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NlxM4Y0D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604403589899/h1-rNxcMU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NlxM4Y0D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604403589899/h1-rNxcMU.png" alt="ConfigEnvironment.png" width="880" height="207"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For each parameter, we need a &lt;strong&gt;key&lt;/strong&gt; in the JSON file, representing one of the possible dynamic variables used by the Insomnia plugin. Create the necessary permits like in figure — keeping in mind that we needed all the parameters to exploit V4 signature, but this is not necessarily true for everyone; thus you can keep only &lt;strong&gt;AwsToken&lt;/strong&gt;—.&lt;/p&gt;

&lt;p&gt;To have access to our plugin's functionalities to insert the correct value for a variable, just start typing "Leapp", and by pressing &lt;strong&gt;cmd + space&lt;/strong&gt; (mac users) or &lt;strong&gt;ctrl + space&lt;/strong&gt; (Windows and Linux users), you'll have access to &lt;strong&gt;two&lt;/strong&gt; features to choose from:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Leapp - AWS Cognito Token&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Leapp - Signature V4&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once selected the plugin's name, this should become highlighted as shown in the figure, and, at that point, it's possible to click on it to have access to configuration parameters. Let's see those of &lt;strong&gt;Aws Cognito Token&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5iUNM-20--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604403607806/LT95nU4V-.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5iUNM-20--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604403607806/LT95nU4V-.png" alt="ConfigAuth.png" width="774" height="750"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You'll be asked to insert parameters like "email" (or username), "password length" (needed to avoid Insomnia making a request for every password character), and another set of parameters retrievable from &lt;strong&gt;Cognito's&lt;/strong&gt; &lt;strong&gt;UserPool&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If everything is set up correctly in Live preview, you'll see the token pre-calculated like in the figure.&lt;/p&gt;

&lt;p&gt;Let's now see IAM's V4 signature parameters. Following again the previous steps, for the same environment, generate a new &lt;strong&gt;JSON object&lt;/strong&gt;, create the &lt;strong&gt;keys&lt;/strong&gt;, and &lt;strong&gt;associate&lt;/strong&gt; the signature feature, then click on it and go to the following screen:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--K73YicEc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604403681385/jmzfgFhEG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K73YicEc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604403681385/jmzfgFhEG.png" alt="ConfigSig.png" width="774" height="486"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On this page, you'll have to set your AWS &lt;strong&gt;config&lt;/strong&gt; and &lt;strong&gt;credentials'&lt;/strong&gt; file path — our plugin defaults it to your home directory —.&lt;/p&gt;

&lt;p&gt;Then we proceed in selecting the Signature V4 property we want to assign to our variable for Insomnia; this procedure &lt;strong&gt;must&lt;/strong&gt; be repeated for Access Id, Secret Key, Session Token, and Region.&lt;/p&gt;

&lt;p&gt;Once the setup is completed, it will be possible to do authenticated API calls by following this procedure:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First thing first, select &lt;strong&gt;AWS&lt;/strong&gt; as a property for "Auth" as shown below:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ykjuBRRh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604403701415/Ccil_DSvt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ykjuBRRh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604403701415/Ccil_DSvt.png" alt="request1.png" width="469" height="51"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Insert parameters as described below:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wnGd5-VC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604403716309/ZMD-HTKbm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wnGd5-VC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604403716309/ZMD-HTKbm.png" alt="where_to_put_config.png" width="469" height="273"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;As a final step, we need to complete Cognito Authentication setting this parameter like in figure:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TE_I4bqr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604403748059/7yQCgnKGW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TE_I4bqr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604403748059/7yQCgnKGW.png" alt="SetCognitoToken.png" width="880" height="203"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You'll have to add &lt;strong&gt;Content-Type&lt;/strong&gt; property with value &lt;strong&gt;application/json&lt;/strong&gt; and our &lt;strong&gt;CognitoToken&lt;/strong&gt; parameter as a &lt;strong&gt;token-id,&lt;/strong&gt; set in an environment with the previous steps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final testing
&lt;/h2&gt;

&lt;p&gt;We are ready to test the call: if all parameters are defined correctly, and your session token is still valid, you can use "Send" to launch your request as we did in the image below to see the response object in the right part of the Insomnia screen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x0-TNM8S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604403772156/IRuyKy1Hm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x0-TNM8S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1604403772156/IRuyKy1Hm.png" alt="tipicalConfigV4Sig.png" width="880" height="358"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Security Considerations
&lt;/h2&gt;

&lt;p&gt;One significant consideration we made was being sure to get sensitive information like credentials from the AWS Credentials' file and not to add them directly from the plugin.&lt;/p&gt;

&lt;p&gt;Moreover, we wanted the credentials' file to persist on the developer's machine only for the time strictly necessary to work with it.&lt;/p&gt;

&lt;p&gt;That's why this plugin is not only a helpful tool for our Leapp software, but it also obtains advantages by using it in combination.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.leapp.cloud/"&gt;Leapp&lt;/a&gt; is our Open-Source software for managing credentials in a multi-cloud environment with a keen eye on security as it can create and delete sensitive information on the fly, avoiding having credentials stored on a user's machine when not needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;In this article, we found the occasion to share with all the readers how an internal need can be the drive to create something useful not only for a particular team but also for the Developer community.&lt;/p&gt;

&lt;p&gt;The topic of splitting as much as possible dependencies between front-end and back-end to increase efficiency and parallelism is always an important one, especially when it comes to testing and/or debugging complex functions.&lt;/p&gt;

&lt;p&gt;As things evolved in times, the front-end is now more complicated than before, and sometimes it requires more coding time than the back-end, mostly if the back-end is done with a &lt;strong&gt;serverless&lt;/strong&gt; approach; this difference can cause the inability of the front-end to serve back-end REST calls when needed.&lt;/p&gt;

&lt;p&gt;Keeping that in mind, we developed this plugin to help us realize Leapp with more ease, and we sincerely hope that our experience can help anyone reading this article and maybe willing to participate in evolving the project even more being it Open-Source.&lt;/p&gt;

&lt;p&gt;Until our next article, stay safe and try Leapp! 🙂&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>aws</category>
      <category>authentication</category>
      <category>insomniaplugin</category>
    </item>
  </channel>
</rss>
