简体   繁体   English

使用来自另一个帐户(us-east-1)的密钥加密 sqs 队列(us-east-2)并使用 lambda 访问它 - 面临错误

[英]Encrypting an sqs queue(us-east-2) using a key from another account(us-east-1) and access it using lambda - facing error

Consider two accounts Account A and Account B. We have to Encrypt an SQS queue in account B using a KMS key from account A and then, send and receive message to the queue using a lambda(which is in account A).考虑两个帐户帐户 A 和帐户 B。我们必须使用帐户 A 中的 KMS 密钥加密帐户 B 中的 SQS 队列,然后使用 lambda(在帐户 A 中)向队列发送和接收消息。 SQS CFN TEMPLATE: SQS CFN 模板:

MyQueue:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: !Ref QueueName
      DelaySeconds: '0'
      MaximumMessageSize: '262144'
      MessageRetentionPeriod: '345600'
      ReceiveMessageWaitTimeSeconds: '0'
      VisibilityTimeout: '30'
      KmsMasterKeyId: <Key_id of the custom CMK> (I have a doubt here also, should i input the key id or the alias of my key?)

KEY POLICY:关键政策:

{
    "Version": "2012-10-17",
    "Id": "key-consolepolicy-3",
    "Statement": [
        {
            "Sid": "Enable IAM User Permissions",
            "Effect": "Allow",
            "Principal": {
                "AWS": [
                    "arn:aws:iam::<Account_B_id>:root",
                    "arn:aws:iam::<Account_A_id>:root",
                    "arn:aws:iam::<Account_A_id>:role/lambda-execution-role"
                ]
            },
            "Action": "kms:*",
            "Resource": "*"
        },
        {
            "Sid": "Allow access for Key Administrators",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::<Account_A_id>:role/lambda-execution-role"
            },
            "Action": [
                "kms:Create*",
                "kms:Describe*",
                "kms:Enable*",
                "kms:List*",
                "kms:Put*",
                "kms:Update*",
                "kms:Revoke*",
                "kms:Disable*",
                "kms:Get*",
                "kms:Delete*",
                "kms:TagResource",
                "kms:UntagResource",
                "kms:ScheduleKeyDeletion",
                "kms:CancelKeyDeletion",
                "kms:ReplicateKey",
                "kms:UpdatePrimaryRegion"
            ],
            "Resource": "*"
        },
        {
            "Sid": "Allow use of the key",
            "Effect": "Allow",
            "Principal": {
                "AWS": [
                    "arn:aws:iam::<Account_B_id>:root",
                    "arn:aws:iam::<Account_A_id>:role/lambda-execution-role"
                ]
            },
            "Action": [
                "kms:Encrypt",
                "kms:Decrypt",
                "kms:ReEncrypt*",
                "kms:GenerateDataKey*",
                "kms:DescribeKey"
            ],
            "Resource": "*"
        },
        {
            "Sid": "Allow attachment of persistent resources",
            "Effect": "Allow",
            "Principal": {
                "AWS": [
                    "arn:aws:iam::<Account_B_id>:root",
                    "arn:aws:iam::<Account_A_id>:role/lambda-execution-role"
                ]
            },
            "Action": [
                "kms:CreateGrant",
                "kms:ListGrants",
                "kms:RevokeGrant"
            ],
            "Resource": "*",
            "Condition": {
                "Bool": {
                    "kms:GrantIsForAWSResource": "true"
                }
            }
        }
    ]
}

SQS Queue policy: SQS 队列策略:


{
  "Version": "2008-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::<Account_A_id>:role/lambda-execution-role"
      },
      "Action": "sqs:*",
      "Resource": "arn:aws:sqs:us-east-2:<Account_B_id>:queue"
    }
  ]
}

Lambda function - Python code for send and receive message to the queue: Lambda function - Python 向队列发送和接收消息的代码:

import json
import boto3
def lambda_handler(event, context):
    sqs = boto3.client('sqs', region_name='us-east-2')
    queue_url = 'https://sqs.us-east-2.amazonaws.com/<Account_B_id>/queue'
    response = sqs.send_message(QueueUrl=queue_url,DelaySeconds=0,MessageBody=('hey there 123 !'))
    response1 = sqs.send_message(QueueUrl=queue_url,DelaySeconds=0,MessageBody=('hey there 1234 !'))
    response2 = sqs.send_message(QueueUrl=queue_url,DelaySeconds=0,MessageBody=('hey there 123345 !'))
    print('message sent')
    response5 = sqs.receive_message(
    QueueUrl=queue_url,
    AttributeNames=['All'],
    MaxNumberOfMessages=10,
    WaitTimeSeconds=7)
    message = response5['Messages'][0]['Body']
    print(message) 

After doing all these things, I have created a test event in lambda and tested it.完成所有这些事情后,我在 lambda 中创建了一个测试事件并对其进行了测试。 Getting the below error:出现以下错误:

[ERROR] ClientError: An error occurred (KMS.AccessDeniedException) when calling the SendMessage operation: null (Service: AWSKMS; Status Code: 400; Error Code: AccessDeniedException; Request ID: d6913dbc-e22f-4ccf-ba5a-9844ab1156e0; Proxy: null)
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 6, in lambda_handler
    response = sqs.send_message(QueueUrl=queue_url,DelaySeconds=0,MessageBody=('hey there 123 !'))
  File "/var/runtime/botocore/client.py", line 386, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/var/runtime/botocore/client.py", line 705, in _make_api_call
    raise error_class(parsed_response, operation_name)

Without KMS this set up is working.在没有 KMS 的情况下,此设置可以正常工作。 Can someone help me out for integrating the KMS in this?有人可以帮助我将 KMS 集成到其中吗?

For resolving this, i reached out to AWS Support and got the solution.为了解决这个问题,我联系了 AWS Support 并获得了解决方案。 Initially i had my key in us-east-1 and trying to use it.最初我把钥匙放在 us-east-1 并尝试使用它。 In order to access an SQS in us-east-2, the key must also be in us-east-2.为了访问 us-east-2 中的 SQS,密钥也必须在 us-east-2 中。 So the solution suggested to me was, Create a replica of the key in us-east-2 and input the arn of the replica key (I did a mistake here too, i gave the key id in the cfn template) in the cloudformation template (Since i was using a multi-region key) .所以向我建议的解决方案是,在 us-east-2 中创建密钥的副本并在 cloudformation 模板中输入副本密钥的 arn (我在这里也犯了一个错误,我在 cfn 模板中给出了密钥 id) (因为我使用的是多区域密钥) If it isn't a multi-region key, we must create a new key in the same region of the SQS queue.如果它不是多区域密钥,我们必须在 SQS 队列的同一区域中创建一个新密钥。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS 在 us-east-1 之外使用 CloudFront 和 HTTPS - AWS Using CloudFront and HTTPS outside us-east-1 提供的区域名称“美国东部(俄亥俄)us-east-2”与支持的格式不匹配 - Provided region_name 'US East (Ohio) us-east-2' doesn't match a supported format Node.js 与 SQS:访问资源 https://sqs.us-east-1.amazonaws.com/ 被拒绝 - Node.js with SQS: Access to the resource https://sqs.us-east-1.amazonaws.com/ is denied 带有 django-storages 的 ImageField 导致:“解析 X-Amz-Credential 参数时出错;区域‘us-east-1’错误;期待‘us-west-1’” - ImageField with django-storages leads to: "Error parsing the X-Amz-Credential parameter; the region 'us-east-1' is wrong; expecting 'us-west-1'" 默认 s3.amazonaws.com 位于 us-east-1 以外的区域 - Have default s3.amazonaws.com to be on other region than us-east-1 “请切换到‘us-east1-dialogflow.googleapis.com’以访问位于‘us-east1’的资源” Dialogflow CX GO API V3 - "Please switch to 'us-east1-dialogflow.googleapis.com' to access resources located in 'us-east1'" Dialogflow CX GO API V3 AWS Cloudfront:指定的 SSL 证书不存在、不在 us-east-1 区域、无效或不包含有效的证书链 - AWS Cloudfront: The specified SSL certificate doesn't exist, isn't in us-east-1 region, isn't valid, or doesn't include a valid certificate chain 为什么会出现此错误? UnknownEndpoint:无法访问的主机:`devicefarm.us-east-1.amazonaws.com' - Why am I getting this error? UnknownEndpoint: Inaccessible host: `devicefarm.us-east-1.amazonaws.com' AWS Glacier 美国东部最便宜。 如果我用它来存储来自印度的数据有什么缺点吗? - AWS Glacier US East is cheapest. Any drawbacks if I use that to store data from India? “cognito-idp.us-east-1.amazonaws.com”的自定义域 - Custom domain for "cognito-idp.us-east-1.amazonaws.com"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM