我有一个案例,我将一些许可证密钥作为云形成模板的输入,然后我必须使用它来创建消防水带传输流。 这工作正常,因为我可以将许可证密钥直接传递到 Firehose 传输流创建逻辑,如下所示: 参数如下:
Parameters:
LicenseKey:
Type: String
Decription: Some description
这是 Firehose 传输流中的代码片段:
StreamNameXyzabc:
Type: AWS::KinesisFirehose::DeliveryStream
Properties:
HttpEndpointDestinationConfiguration:
RequestConfiguration:
ContentEncoding: GZIP
EndpointConfiguration:
Name: xyz
Url : 'some url'
Accesskey: !Ref Licensekey
上面的代码运行良好。我能够发送日志。 现在,如果我想将许可证密钥存储在秘密管理器中,同时以秘密管理器的名义使用 stackId,我正在执行以下操作:
LicenseKeySecret:
Type: 'AWS::SecretsManager::Secret'
Properties:
Description:
Name : !Join ['-', ['himanshu-', !Select [4, !Split ['-', !Select [2, !Split ['/', !Ref AWS::StackId]]]]]]
SecretString: !Sub '{ "LicenseKey": "${LicenseKey}"}'
我想在上面提到的 FireHoseDelivery Stream 代码中使用这个秘密管理器:
EndpointConfiguration:
Name: xyz
Url : 'some url'
Accesskey: !Ref Licensekey
到目前为止我已经尝试了多种方式访问它但无法访问它,如何访问它。 我尝试过的一些方法:
1. AccessKey: Fn::Subresolve:secretsmanager:himanshu-${AWS::StackId}-LicenseKey}}
2. AccessKey: !Sub '{{resolve:secretsmanager:himanshu-key-${AWS::StackId}:SecretString:LicenseKey}}'
以及更多的排列组合,在某些情况下堆栈创建本身失败,但在某些情况下堆栈创建并未失败,但 LicenseKey 不正确,因此 firehose 无法发送日志。
我需要回答以下两个问题:
FirehoseAccessKeyLoggerLambda:
Type: 'AWS::Serverless::Function'
DependsOn: someResource
Properties:
InlineCode: |
import json
import boto3
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def handler(event, context):
firehose_client = boto3.client('firehose')
stream_name = event['ResourceProperties']['StreamName']
try:
response = firehose_client.describe_delivery_stream(DeliveryStreamName=stream_name)
logger.info(f'Response: {response}')
#access_key = response['DeliveryStreamDescription']['Destinations'][0]['HttpEndpointDestinationDescription']['AccessKey']
#logger.info(f'Access Key: {access_key}')
#accessKey is not present in the response it seems.
except Exception as e:
logger.error(f'Error retrieving access key: {str(e)}')
raise e
Handler: index.handler
Runtime: python3.12
Role: !GetAtt FirehoseAccessKeyLoggerLambdaExecutionRole.Arn
Timeout: 120
MemorySize: 128
如果您的 LicenseKeySecret 资源如下所示:
LicenseKeySecret:
Type: 'AWS::SecretsManager::Secret'
Properties:
Description: License Key Secret
Name: !Join ['-', ['himanshu-', !Select [4, !Split ['-', !Select [2, !Split ['/', !Ref AWS::StackId]]]]]]
SecretString: !Sub '{"LicenseKey": "${LicenseKey}"}'
您可以在 Firehose AccessKey 属性中引用密钥的值,如下所示:
StreamNameXyzabc:
Type: AWS::KinesisFirehose::DeliveryStream
Properties:
HttpEndpointDestinationConfiguration:
RequestConfiguration:
ContentEncoding: GZIP
EndpointConfiguration:
Name: xyz
Url: 'some url'
AccessKey: !Sub '{{resolve:secretsmanager:${LicenseKeySecret.Name}:SecretString:LicenseKey}}'
LicenseKeySecret 的名称是动态生成的,因此使用 .Name 可确保您正确引用它。 如果有任何问题,请写出完整的代码。谢谢