[AWS] Lab: Configure a Cognito user can access DynamoDB for read

 

Create an IAM User

Give Admin access

Create DynamoDB Table

复制代码
  
1) Create facts table:
**** (use ^ - Shift + 6 if you are a windows user, not \) ****

aws dynamodb create-table --table-name facts --attribute-definitions \
AttributeName=fact_id,AttributeType=N --key-schema \
AttributeName=fact_id,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5


2) Populate facts table:
**** (make sure items.json is in your working directory) ****

aws dynamodb batch-write-item --request-items file://items.json
复制代码

items.json

https://github.com/ACloudGuru-Resources/course-aws-certified-developer-associate/blob/main/Cognito_Demo/items.json

Create an EC2 Instance

Install HTTPD:

#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd

Create a Cognito Identity Pool and IAM Role

复制代码
Cognito Commands: 


1) Using the CLI, create new identity pool, named DynamoPool, allow unauthenticated entities.
**** (use ^ - Shift + 6 if you are a windows user, not \) ****

    aws cognito-identity create-identity-pool \
    --identity-pool-name DynamoPool \
    --allow-unauthenticated-identities \
    --output json
       

2) Create an IAM role named Cognito_DynamoPoolUnauth. 

aws iam create-role --role-name Cognito_DynamoPoolUnauth --assume-role-policy-document file://myCognitoPolicy.json --output json
 
3) Grant the Cognito_DynamoPoolUnauth role read access to DynamoDB by attaching a managed policy (AmazonDynamoDBReadOnlyAccess).

aws iam attach-role-policy --policy-arn arn:aws:iam::aws:policy/AmazonDynamoDBReadOnlyAccess --role-name Cognito_DynamoPoolUnauth 

4) Get the IAM role Amazon Resource Name (ARN).
aws iam get-role --role-name Cognito_DynamoPoolUnauth --output json 


5) Add our role to the Cognito Identity Pool. Replace the pool ID with your own pool ID and use the role ARN from the previous step.

aws cognito-identity set-identity-pool-roles \
--identity-pool-id "us-east-1:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
--roles unauthenticated=arn:aws:iam::xxxxx:role/Cognito_DynamoPoolUnauthRole --output json

6) Double check it worked using: 

aws cognito-identity get-identity-pool-roles  --identity-pool-id "us-east-1:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

7) We can now specify the Cognito credentials in our application - i.e. in the JavaScript section of our webpage!
Replace the identity pool ID with your own and the role ARN with your own role ARN. 
We are going to add this snippet to our index.html:


AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: "us-east-1:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
RoleArn: "arn:aws:iam::xxxxx:role/Cognito_DynamoPoolUnauthRole"
});
复制代码
posted @   Zhentiw  阅读(178)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-04-23 [Docker] Benefits of Multi-stage Builds
2019-04-23 [Mockito] Mock List interface
2018-04-23 [Angular] Upgrading to RxJS v6
2017-04-23 [Linux] Search the contents of files using grep
点击右上角即可分享
微信分享提示