认证考题 | 控制对 Amazon S3 存储桶的访问权限


  题目

A company has an AWS Lambda function that creates image thumbnails from larger images. The Lambda function needs read and write access to an Amazon S3 bucket in the same AWS account.
Which solutions will provide the Lambda function this access? (Choose two.)
A. Create an IAM user that has only programmatic access. Create a new access key pair. Add environmental variables to the Lambda function with the access key ID and secret access key. Modify the Lambda function to use the environmental variables at run time during communication with Amazon S3.
B. Generate an Amazon EC2 key pair. Store the private key in AWS Secrets Manager. Modify the Lambda function to retrieve the private key from Secrets Manager and to use the private key during communication with Amazon S3.
C. Create an IAM role for the Lambda function. Attach an IAM policy that allows access to the S3 bucket.
D. Create an IAM role for the Lambda function. Attach a bucket policy to the S3 bucket to allow access. Specify the function's IAM role as the principal.
E. Create a security group. Attach the security group to the Lambda function. Attach a bucket policy that allows access to the S3 bucket through the security group ID.

  参考答案

CD

  需求概括

一家公司有一个AWS Lambda函数,用于从大图像创建缩略图。该Lambda函数需要对同一AWS账户中的Amazon S3存储桶进行读写访问。
如何为Lambda函数配置对S3存储桶的读写权限?(选择两项)

  参考解析

技巧:排除明显错误选项,在没有明显错误的选项中选择最合理的选项。

A. 不正确。创建一个仅具有程序化访问权限的IAM用户。创建新的访问密钥对。将访问密钥ID和秘密访问密钥作为环境变量添加到Lambda函数中。修改Lambda函数以在运行时使用环境变量与Amazon S3通信。硬编码长期访问密钥存在安全风险(如密钥泄露),不符合AWS最佳实践(推荐使用临时凭证)。
B. 不正确。生成Amazon EC2密钥对。将私钥存储在AWS Secrets Manager中。修改Lambda函数以从Secrets Manager检索私钥,并在与Amazon S3通信时使用私钥。EC2密钥对用于SSH登录,与S3 API访问无关,且Secrets Manager不适用于此场景。
C. 正确。为Lambda函数创建IAM角色。附加允许访问S3存储桶的IAM策略。通过执行角色授予临时凭证,策略可精确控制权限,是AWS推荐方式。
D. 正确。为Lambda函数创建IAM角色。将存储桶策略附加到S3存储桶以允许访问。指定函数的IAM角色作为主体。存储桶策略可显式允许角色访问,与IAM策略形成双重控制(需确保策略不冲突)。
E. 不正确。创建安全组。将安全组附加到Lambda函数。将允许通过安全组ID访问S3存储桶的存储桶策略附加到存储桶。Lambda默认不在VPC内运行,安全组不生效;即使配置VPC,安全组也无法直接控制S3访问权限(需通过IAM或存储桶策略)。

  技术总结

IAM角色:Lambda可通过执行角色(Execution Role)获取临时凭证,无需硬编码密钥,符合最小权限原则。
IAM策略:直接附加到角色的策略可精确控制对S3存储桶的读写操作(如s3:GetObject和s3:PutObject)。
存储桶策略:可通过存储桶策略显式允许特定IAM角色或用户访问,但需与IAM策略配合使用(如允许角色作为主体)。
密钥管理:AWS Secrets Manager:适用于存储数据库凭证等敏感信息,但Lambda访问S3无需长期密钥,使用临时凭证更安全。
网络控制:安全组:仅适用于VPC内的资源(如EC2实例),Lambda默认在VPC外运行,安全组无法直接控制其访问S3的权限。