[CLF-C02 Practice Questions Q29] Granting Permissions to an Application on EC2 (IAM Roles)

CLF-C02 Practice Questions Q29 Granting Permissions to an Application on EC2 (IAM Roles) Certification

“Why isn’t it enough to allow the traffic with a security group when an EC2 instance writes to DynamoDB?”
“I thought writing the access key in a configuration file was the most reliable approach.”

AWS Certified Cloud Practitioner (exam code CLF-C02, CLF below) asks questions that have you choose how to give an application on EC2 permissions without placing an access key on the instance. The key points behind the wrong options are the difference between allowing traffic and having permission to act, and why access keys for the root user or an IAM user should be avoided.

This article is Question 29 of a series that explains CLF practice questions one at a time. The author wrote the questions to match the scope of the exam; they are not actual exam questions. Choose your answer first, then read the explanation.


Question

A company has an application running on an Amazon EC2 instance that writes data to an Amazon DynamoDB table. It wants to give the application only the permissions it needs, without placing a long-term access key on the instance. Which is the most appropriate method?

  • A. Use an outbound rule of the security group to allow traffic to DynamoDB
  • B. Set the access key of the AWS account root user in an environment variable on the instance
  • C. Create an IAM role that allows only writes to the DynamoDB table, and attach it to the instance
  • D. Create an IAM user, issue an access key, and write it in the application’s configuration file

Once you have chosen your answer, open the section below.

Show the answer (click to open)

Answer: C

When you attach an IAM role that allows only writes to the instance, the application automatically receives temporary credentials. You can give it only the permissions it needs, without placing an access key.


The answer is C

The correct answer is C.

You attach an IAM role that allows only writes to the instance. The application automatically receives temporary credentials, so you do not need to place an access key. Let’s look at the options one at a time.


Explanation of each option

A: Allowing traffic is not permission (incorrect)

At the upper left, an application on an EC2 instance goes through a security group that allows outbound traffic toward DynamoDB, but the write is denied. At the lower left, to write, both checks must pass: "Is traffic allowed?" (security group) is allowed, so it passes; "Is the action allowed?" (IAM permissions) is not granted, so it is denied. On the right: "Allowing traffic and permission to act differ" and "Grants no permission"

A security group is a network gate that decides whether traffic passes. To write data to DynamoDB, in addition to the traffic getting through, you need IAM permissions.

As in the diagram, even if an outbound rule allows the traffic, the write is denied if there are no IAM permissions. Allowing traffic and having permission to act are separate things.

B: Do not use root user keys (incorrect)

At the upper left, the root user's access key is placed in an environment variable of the EC2 instance, so it runs as the root user that can do everything in the account. The operations this key can do are writing to DynamoDB (the only one needed, in the green box), plus deleting a DynamoDB table, terminating EC2, deleting S3 data, creating IAM users, viewing billing information, deleting an RDS DB, and closing the account. On the right: "More than needed"

The root user is a special user that can do everything in an AWS account. As in the diagram, if you use its access key, the application can do anything, up to deleting the table or closing the account. This goes against the requirement of only the permissions it needs.

As a rule, you do not create an access key for the root user. It is a long-term key, so if it leaks, everything in the account is at risk.

C: Attach an IAM role (correct)

At the upper left, an IAM role (policy: allows only writes to the DynamoDB table) is attached to the EC2 instance through an instance profile. The application (AWS SDK, CLI) writes to the DynamoDB table. Temporary credentials are delivered automatically via instance metadata and are rotated automatically by AWS. On the right: "Least privilege," with no access key on the instance

You attach an IAM role that allows only writes to the instance. The mechanism is called an instance profile.

The application receives temporary credentials from instance metadata. The AWS SDK and CLI use them automatically. The temporary credentials are rotated automatically by AWS. If you change the role’s policy, credentials with the new permissions are delivered automatically.

You do not need to place an access key, and the permissions are limited to what is needed.

D: Do not place a long-term access key (incorrect)

An access key issued to an IAM user is written in the application's configuration file (access_key and secret_key) on an EC2 instance. It can leak through published code, backups, and so on, and a third party can use it at any time with the same permissions. Long-term credentials can be used until you delete or deactivate them, and you must rotate them yourself. On the right: "Breaks the requirement," so use an IAM role for EC2

An IAM user’s access key is a long-lived credential. You can keep using it until you delete or deactivate it.

If you write it in a configuration file, then, as in the diagram, it can be abused by a third party if it leaks through published code or backups. It also goes against the requirement not to place a long-term access key in the first place. For an application on EC2, use an IAM role.


Long-term and temporary credentials

Here is a table of the differences between long-term and temporary credentials. The big differences are whether you need to place them and who performs the rotation.

ItemLong-term credentialsTemporary credentials
ExamplesAccess keys of an IAM user or the root userAn IAM role (an EC2 instance profile)
Where they liveYou place them yourself in a configuration file, an environment variable, etc.You do not place them. The instance receives them from instance metadata
How long they workUntil you delete or deactivate themThey have an expiration
RotationYou do it yourselfAWS does it automatically

Key points

  • Giving an application on EC2 permissions = attach an IAM role
  • Temporary credentials are delivered and rotated automatically
  • Do not place access keys in configuration files or environment variables
  • A security group allows traffic. Permission to act comes from IAM

The course articles explain how IAM roles work and why you should not create access keys for the root user in detail.

  • Lecture 10, Part 5: IAM and least privilege, in the “IAM roles” section
  • Lecture 10, Part 4: AWS accounts and the root user, in the “How to protect the root user” section

You can read all 14 lectures of AWS Cloud Practitioner Exam Prep in order from its series page.


The CLF-C02 Practice Questions series (100 questions)

All 100 questions with a reading guide: CLF-C02 Practice Questions

In this group (Questions 19–30: The shared responsibility model and security):

This series is the English edition of 【CLF-C02 試験対策問題】, originally written in Japanese.

コメント

Copied title and URL