步骤 1:创建无服务器缓存 - 亚马逊 ElastiCache (Redis OSS)
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

步骤 1:创建无服务器缓存

要创建无服务器缓存,请按照以下步骤操作。

步骤 1.1:创建无服务器缓存

在此步骤中,您将使用 (CLI) 在账户的 us-east-1 区域的默认 Amazon VPC 中创建无服务器缓存。 Amazon Command Line Interface 有关使用 ElastiCache 控制台或 API 创建无服务器缓存的信息,请参阅第 1 步:创建缓存

aws elasticache create-serverless-cache \ --serverless-cache-name cache-01 \ --description "ElastiCache IAM auth application" \ --engine redis

请注意,“状态”字段的值设置为 CREATING。可能需要一分钟 ElastiCache 才能完成缓存的创建。

步骤 1.2:复制无服务器缓存端点

使用describe-serverless-caches命令验证 ElastiCache (Redis OSS)是否已完成缓存的创建。

aws elasticache describe-serverless-caches \ --serverless-cache-name cache-01

复制输出中显示的端点地址。在为 Lambda 函数创建部署包时,您将需要此地址。

步骤 1.3:创建 IAM 角色

  1. 为您的角色创建 IAM 信任政策文档,如下所示,允许您的账户承担新角色。将策略保存到名为 trust-policy.json 的文件中。

    { "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::123456789012:root" }, "Action": "sts:AssumeRole" }, { "Effect": "Allow", "Principal": { "Service": "lambda.amazonaws.com" }, "Action": "sts:AssumeRole" }] }
  2. 创建 IAM policy 文档,如下所示。将策略保存到名为 policy.json 的文件中。

    { "Version": "2012-10-17", "Statement": [ { "Effect" : "Allow", "Action" : [ "elasticache:Connect" ], "Resource" : [ "arn:aws:elasticache:us-east-1:123456789012:serverlesscache:cache-01", "arn:aws:elasticache:us-east-1:123456789012:user:iam-user-01" ] } ] }
  3. 创建一个 IAM 角色。

    aws iam create-role \ --role-name "elasticache-iam-auth-app" \ --assume-role-policy-document file://trust-policy.json
  4. 创建 IAM 策略。

    aws iam create-policy \ --policy-name "elasticache-allow-all" \ --policy-document file://policy.json
  5. 向角色附加 IAM policy。

    aws iam attach-role-policy \ --role-name "elasticache-iam-auth-app" \ --policy-arn "arn:aws:iam::123456789012:policy/elasticache-allow-all"

步骤 1.4:创建无服务器缓存

  1. 创建新的默认用户。

    aws elasticache create-user \ --user-name default \ --user-id default-user-disabled \ --engine redis \ --authentication-mode Type=no-password-required \ --access-string "off +get ~keys*"
  2. 创建启用 IAM 的新用户。

    aws elasticache create-user \ --user-name iam-user-01 \ --user-id iam-user-01 \ --authentication-mode Type=iam \ --engine redis \ --access-string "on ~* +@all"
  3. 创建用户组并附加用户。

    aws elasticache create-user-group \ --user-group-id iam-user-group-01 \ --engine redis \ --user-ids default-user-disabled iam-user-01 aws elasticache modify-serverless-cache \ --serverless-cache-name cache-01 \ --user-group-id iam-user-group-01