Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add role_last_used attribute to iam_role resource #30750

Merged
merged 5 commits into from
Apr 14, 2023

Conversation

dpowley
Copy link
Contributor

@dpowley dpowley commented Apr 14, 2023

Description

This PR adds the role_last_used attribute to the aws_iam_role resource and datasource.

Relations

No issue that I could find relating to this.

References

https://docs.aws.amazon.com/IAM/latest/APIReference/API_RoleLastUsed.html

Output from Acceptance Testing

This one is tough to add an acceptance test for because in order to have that value populated, you would need to create a role and then use it and then wait for AWS to populate the last used information (which takes about 15-30 min from what I've seen).

I did run the test locally for both the role and datasource and got the following on create:

 terraform state show aws_iam_role.role
# aws_iam_role.role:
resource "aws_iam_role" "role" {
    arn                   = "arn:aws:iam::XXXXXXXXXXXXX:role/test6"
    assume_role_policy    = jsonencode(
        {
            Statement = [
                {
                    Action    = [
                        "sts:AssumeRole",
                        "sts:TagSession",
                        "sts:SetSourceIdentity",
                    ]
                    Effect    = "Allow"
                    Principal = {
                        AWS = "arn:aws:iam::XXXXXXXXXXXXX:role/aws_devon_test-developer"
                    }
                },
            ]
            Version   = "2012-10-17"
        }
    )
    create_date           = "2023-04-14T18:27:54Z"
    force_detach_policies = false
    id                    = "test6"
    managed_policy_arns   = []
    max_session_duration  = 3600
    name                  = "test6"
    path                  = "/"
    tags_all              = {}
    unique_id             = "AROAXXXXXXXXXXXXX"

    inline_policy {
        name   = "my_inline_policy"
        policy = jsonencode(
            {
                Statement = [
                    {
                        Action   = [
                            "ec2:Describe*",
                        ]
                        Effect   = "Allow"
                        Resource = "*"
                    },
                ]
                Version   = "2012-10-17"
            }
        )
    }
}

Then got the following for the datasource (before using the role):

> terraform state show data.aws_iam_role.test
# data.aws_iam_role.test:
data "aws_iam_role" "test" {
    arn                  = "arn:aws:iam::XXXXXXXXXXXX:role/test6"
    assume_role_policy   = jsonencode(
        {
            Statement = [
                {
                    Action    = [
                        "sts:AssumeRole",
                        "sts:TagSession",
                        "sts:SetSourceIdentity",
                    ]
                    Effect    = "Allow"
                    Principal = {
                        AWS = "arn:aws:iam::XXXXXXXXXXXX:role/aws_devon_test-developer"
                    }
                },
            ]
            Version   = "2012-10-17"
        }
    )
    create_date          = "2023-04-14T18:27:54Z"
    id                   = "test6"
    max_session_duration = 3600
    name                 = "test6"
    path                 = "/"
    role_last_used       = [
        {
            last_used_date = "2023-04-14T18:30:03Z"
            region         = "us-east-1"
        },
    ]
    tags                 = {}
    unique_id            = "AROAXXXXXXXXXXXX"
}

I then used the role, waited a while and got the following when re-applying:
Screenshot 2023-04-14 at 3 56 39 PM

The updated state for the resource:

 terraform state show aws_iam_role.role
# aws_iam_role.role:
resource "aws_iam_role" "role" {
    arn                   = "arn:aws:iam::XXXXXXXXXXXX:role/test6"
    assume_role_policy    = jsonencode(
        {
            Statement = [
                {
                    Action    = [
                        "sts:AssumeRole",
                        "sts:TagSession",
                        "sts:SetSourceIdentity",
                    ]
                    Effect    = "Allow"
                    Principal = {
                        AWS = "arn:aws:iam::XXXXXXXXXXXX:role/aws_devon_test-developer"
                    }
                },
            ]
            Version   = "2012-10-17"
        }
    )
    create_date           = "2023-04-14T18:27:54Z"
    force_detach_policies = false
    id                    = "test6"
    managed_policy_arns   = []
    max_session_duration  = 3600
    name                  = "test6"
    path                  = "/"
    role_last_used        = [
        {
            last_used_date = "2023-04-14T18:30:03Z"
            region         = "us-east-1"
        },
    ]
    tags                  = {}
    tags_all              = {}
    unique_id             = "AROAXXXXXXXXXXXX"

    inline_policy {
        name   = "my_inline_policy"
        policy = jsonencode(
            {
                Statement = [
                    {
                        Action   = [
                            "ec2:DescribeRegion",
                        ]
                        Effect   = "Allow"
                        Resource = "*"
                    },
                ]
                Version   = "2012-10-17"
            }
        )
    }
}

The updated state for the datasource:

> terraform state show data.aws_iam_role.test
# data.aws_iam_role.test:
data "aws_iam_role" "test" {
    arn                  = "arn:aws:iam::XXXXXXXXXXXX:role/test6"
    assume_role_policy   = jsonencode(
        {
            Statement = [
                {
                    Action    = [
                        "sts:AssumeRole",
                        "sts:TagSession",
                        "sts:SetSourceIdentity",
                    ]
                    Effect    = "Allow"
                    Principal = {
                        AWS = "arn:aws:iam::XXXXXXXXXXXX:role/aws_devon_test-developer"
                    }
                },
            ]
            Version   = "2012-10-17"
        }
    )
    create_date          = "2023-04-14T18:27:54Z"
    id                   = "test6"
    max_session_duration = 3600
    name                 = "test6"
    path                 = "/"
    role_last_used       = [
        {
            last_used_date = "2023-04-14T18:30:03Z"
            region         = "us-east-1"
        },
    ]
    tags                 = {}
    unique_id            = "AROAXXXXXXXXXXXX"
}

@github-actions
Copy link

Community Note

Voting for Prioritization

  • Please vote on this pull request by adding a 👍 reaction to the original post to help the community and maintainers prioritize this pull request.
  • Please see our prioritization guide for information on how we prioritize.
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request.

For Submitters

  • Review the contribution guide relating to the type of change you are making to ensure all of the necessary steps have been taken.
  • For new resources and data sources, use skaff to generate scaffolding with comments detailing common expectations.
  • Whether or not the branch has been rebased will not impact prioritization, but doing so is always a welcome surprise.

@github-actions github-actions bot added documentation Introduces or discusses updates to documentation. service/iam Issues and PRs that pertain to the iam service. size/M Managed by automation to categorize the size of a PR. needs-triage Waiting for first response or review from a maintainer. labels Apr 14, 2023
@jar-b jar-b removed the needs-triage Waiting for first response or review from a maintainer. label Apr 14, 2023
@dpowley dpowley requested a review from jar-b April 14, 2023 20:05
@jar-b jar-b self-assigned this Apr 14, 2023
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome @dpowley 👋

It looks like this is your first Pull Request submission to the Terraform AWS Provider! If you haven’t already done so please make sure you have checked out our CONTRIBUTOR guide and FAQ to make sure your contribution is adhering to best practice and has all the necessary elements in place for a successful approval.

Also take a look at our FAQ which details how we prioritize Pull Requests for inclusion.

Thanks again, and welcome to the community! 😃

Copy link
Member

@jar-b jar-b left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉

$ make testacc PKG=iam TESTS=TestAccIAMRole_
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./internal/service/iam/... -v -count 1 -parallel 20 -run='TestAccIAMRole_'  -timeout 180m

--- PASS: TestAccIAMRole_badJSON (3.87s)
=== CONT  TestAccIAMRole_nameGenerated
--- PASS: TestAccIAMRole_InlinePolicy_empty (27.04s)                                                                                                                                                           === CONT  TestAccIAMRole_namePrefix                                                                                                                                                                            --- PASS: TestAccIAMRole_basic (29.32s)
=== CONT  TestAccIAMRole_InlinePolicy_ignoreOrder
--- PASS: TestAccIAMRole_policiesForceDetach (29.95s)
=== CONT  TestAccIAMRole_disappears
--- PASS: TestAccIAMRole_nameGenerated (28.46s)
=== CONT  TestAccIAMRole_description
--- PASS: TestAccIAMRole_ManagedPolicy_outOfBandAdditionIgnored (46.91s)
--- PASS: TestAccIAMRole_ManagedPolicy_outOfBandRemovalAddedBack (47.87s)
--- PASS: TestAccIAMRole_ManagedPolicy_outOfBandAdditionRemoved (48.13s)
--- PASS: TestAccIAMRole_InlinePolicy_outOfBandAdditionRemovedEmpty (48.39s)
--- PASS: TestAccIAMRole_ManagedPolicy_outOfBandAdditionRemovedEmpty (48.41s)
--- PASS: TestAccIAMRole_InlinePolicy_outOfBandAdditionRemoved (49.22s)
--- PASS: TestAccIAMRole_InlinePolicy_outOfBandRemovalAddedBack (49.66s)
--- PASS: TestAccIAMRole_testNameChange (50.88s)
--- PASS: TestAccIAMRole_tags (51.32s)
--- PASS: TestAccIAMRole_disappears (22.75s)
--- PASS: TestAccIAMRole_namePrefix (27.63s)
--- PASS: TestAccIAMRole_maxSessionDuration (55.11s)
--- PASS: TestAccIAMRole_InlinePolicy_outOfBandAdditionIgnored (57.35s)
--- PASS: TestAccIAMRole_InlinePolicy_basic (62.27s)
--- PASS: TestAccIAMRole_ManagedPolicy_basic (62.31s)
--- PASS: TestAccIAMRole_InlinePolicy_ignoreOrder (40.83s)
--- PASS: TestAccIAMRole_description (42.03s)
--- PASS: TestAccIAMRole_permissionsBoundary (77.28s)
--- PASS: TestAccIAMRole_diffsCondition (79.48s)
--- PASS: TestAccIAMRole_diffs (186.46s)
PASS
ok      github.com/hashicorp/terraform-provider-aws/internal/service/iam        189.688s
$ make testacc PKG=iam TESTS=TestAccIAMRoleDataSource_
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./internal/service/iam/... -v -count 1 -parallel 20 -run='TestAccIAMRoleDataSource_'  -timeout 180m

--- PASS: TestAccIAMRoleDataSource_tags (10.56s)
--- PASS: TestAccIAMRoleDataSource_basic (10.57s)
PASS
ok      github.com/hashicorp/terraform-provider-aws/internal/service/iam        13.687s

@jar-b jar-b merged commit 1b87ab5 into hashicorp:main Apr 14, 2023
42 checks passed
@jar-b
Copy link
Member

jar-b commented Apr 14, 2023

Thanks for your contribution, @dpowley!

@github-actions github-actions bot added this to the v4.64.0 milestone Apr 14, 2023
@dpowley dpowley deleted the devon/f-iam-role-last-used branch April 17, 2023 13:10
@github-actions
Copy link

This functionality has been released in v4.64.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Introduces or discusses updates to documentation. service/iam Issues and PRs that pertain to the iam service. size/M Managed by automation to categorize the size of a PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants