Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
AWS · DVA-C03

IAM permissions and roles in code

DVA-C03 · Lesson 10 of 24 · 1:24:26

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the DVA-C03 exam (AWS Certified Developer – Associate). In this lesson: the SDK credential provider chain, why to avoid long-term access keys, SigV4 signing, identity-based and resource-based policies, policy evaluation, least privilege with IAM Access Analyzer, AssumeRole and STS, trust policies, session duration, role chaining, and iam:PassRole. Includes three original practice questions.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

A key in the repository

Last year, a developer at AnyCompany Tickets pasted an access key into a script, and the script ended up in a Git repository. Since then, the rule is simple: no long-term keys in code, ever, and every component gets only the permissions that it needs. This lesson covers credentials in code and in the CLI, request signing, identity-based and resource-based policies, least privilege, and roles assumed with AWS STS.

Credentials in code

The AWS SDKs and the CLI look for credentials in a fixed series of places, so the code itself never names a key. On a laptop, environment variables or a named profile in the shared config and credentials files, set up with the CLI, provide them. On AWS, the platform provides temporary credentials of a role: the execution role of a Lambda function, the task role of an ECS task, or the instance profile of an EC2 instance. Access keys are long-term credentials of an IAM user, so prefer roles and temporary credentials, and never commit a key. The same code then runs on the laptop and in Lambda, with different credentials and no change.

Signing requests

Every AWS API request is signed with Signature Version 4, or SigV4. The SDK derives a signing key, scoped to one service, one Region, and one day, and the secret key itself never travels. The signature goes in the Authorization header, or in the query string, as in a presigned URL, and a timestamp limits how long the request stays valid. When your code sends raw HTTP requests, like a call to an API protected by IAM authorization, it signs them with the signer of the SDK.

Identity-based and resource-based policies

An identity-based policy is attached to a user, a group, or a role, and says what that identity can do. A resource-based policy is attached to a resource, like an S3 bucket, an SQS queue, a KMS key, or a Lambda function, and names the principals that can use it. Every request starts denied, an allow in a policy lets it through, and an explicit deny in any policy always wins. For another account, the resource-based policy and the caller's own policy must both allow the action, unless the caller assumes a role in the resource's account. And to let S3 or API Gateway invoke a function, its resource-based policy grants them the invoke permission.

Least privilege

Least privilege means only the actions that a component needs, on only the resources that it uses. So a policy names actions like PutItem and GetItem, instead of every DynamoDB action, and the ARN of one table, instead of a wildcard. Conditions narrow it further, like a required tag or a source VPC endpoint. And IAM Access Analyzer can generate a policy from the activity in CloudTrail, while last accessed information shows the permissions that nobody uses.

Roles and AWS STS

A role has two policies: a trust policy, which says who can assume it, and a permissions policy, which says what it can do. AssumeRole, an AWS STS operation, returns temporary credentials: an access key ID, a secret key, and a session token. A session lasts from fifteen minutes up to the role's maximum, which goes from one hour to twelve, and a role assumed from another role session is limited to one hour. This is how code in one account works in another account, through a role that trusts it. And to give a role to a service, like a new Lambda function, the person who deploys it needs the IAM PassRole permission for that role.

Recap

Let's recap. The SDKs find credentials on their own: a profile on a laptop, and the temporary credentials of a role on AWS, so no key goes in the code. Every request is signed with SigV4, from a derived key, and raw HTTP calls use the signer of the SDK. Identity-based policies say what an identity can do, resource-based policies say who can use a resource, and an explicit deny always wins. Least privilege names actions, resources, and conditions, with help from IAM Access Analyzer. And AssumeRole returns temporary credentials through a trust policy, while PassRole controls who can give a role to a service.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. A developer tests a Python script on a laptop, and the same script must run in a Lambda function, without any change and without any key in the code. What should the developer rely on?

    • An access key written at the top of the script
    • Access keys in the function’s environment variables
    • A separate version of the script for Lambda
    • The SDK’s credential providers and a role
    Show the answer

    The SDK’s credential providers and a role

    The SDK finds a profile on the laptop, and the temporary credentials of the execution role in Lambda, with the same code. “An access key written at the top of the script” would put a long-term key in the code, which is the risk to avoid. “Access keys in the function’s environment variables” would still store a long-term key with the function, where anyone who reads its settings sees it. And “A separate version of the script for Lambda” would make the script differ between the laptop and Lambda.

  2. The developer adds an S3 event notification that should invoke the watermark function, but the notification cannot be saved, because S3 is not allowed to invoke the function. What should the developer add?

    • Read access to the bucket in the execution role
    • A permission in the function’s resource-based policy
    • A bucket policy that allows the function
    • Administrator access for the function’s role
    Show the answer

    A permission in the function’s resource-based policy

    The function's resource-based policy must allow the S3 service to invoke it, for that bucket. “Read access to the bucket in the execution role” would let the function read the bucket, which is the other direction. “A bucket policy that allows the function” would control who reaches the bucket, not what S3 can invoke. And “Administrator access for the function’s role” would give broad rights to the function, and still none to S3.

  3. A role’s policy allows GetObject on the tickets bucket, but every request returns access denied. The bucket policy denies any request that does not come through the VPC endpoint, and the function calls S3 over the internet. Why is access denied?

    • The explicit deny in the bucket policy wins
    • Only a bucket policy can allow access
    • GetObject needs a separate read permission
    • Temporary credentials cannot read objects
    Show the answer

    The explicit deny in the bucket policy wins

    An explicit deny in the bucket policy wins over the allow in the role's policy. “Only a bucket policy can allow access” is wrong, because an identity-based policy can allow access to a bucket in the same account. “GetObject needs a separate read permission” is wrong, because the policy names GetObject, and the problem is the deny. And “Temporary credentials cannot read objects” is wrong, because the role's credentials are temporary but valid.