简体   繁体   English

将 Pulumi S3 Bucket ID 作为参数传递给 AWS Lambda

[英]Passing Pulumi S3 Bucket ID to AWS Lambda as a parameter

I would like to pass the S3 bucket ID as a parameter to AWS Lambda via Pulumi.我想通过 Pulumi 将 S3 存储桶 ID 作为参数传递给 AWS Lambda。 However, Pulumi passes this parameter On Build , so I obtain an Object reference in Lambda instead of the S3 path.但是,Pulumi 传递了这个参数On Build ,所以我在 Lambda 中获得了 Object 引用,而不是 S3 路径。

My code looks like this我的代码看起来像这样

bucket = s3.Bucket(resource_name="bucket", acl="private")
lambda_func = lambda_.Function("Function",
                               role=lambda_role.arn,
                               package_type="Image",
                               image_uri=docker.image_name,
                               image_config={"commands": ["api/main.handler"]},
                               vpc_config={"subnet_ids": private_subnet_ids,
                                           "security_group_ids": [internet_access.id],
                                          },
                               environment=lambda_.FunctionEnvironmentArgs(
                                               variables={"STORE_PATH": f"s3://s{bucket.id}/api"})
                               )

According to Pulumi documentation :根据 Pulumi文档

Outputs that contain strings cannot be used directly in operations such as string concatenation.包含字符串的输出不能直接用于字符串连接等操作。

So I've tried the recommended methods like:所以我尝试了推荐的方法,例如:

bucket_url = Output.all(bucket.id).apply(lambda l: f"http://{l[0]}/")

Or或者

url = Output.concat("http://", bucket.id, "/")

Any help is welcomed.欢迎任何帮助。 Thank you in advance to everyone.提前感谢大家。

The last method should work fine.最后一种方法应该可以正常工作。 Note that bucket.id is just the name of the bucket, you need to build S3 access URL with it, eg注意bucket.id只是bucket的名字,你需要用它构建S3访问URL,例如

url = Output.concat("https://", bucket.id, ".s3.us-west-2.amazonaws.com")
# or url = Output.concat("s3://", bucket.id)

If this still doesn't do what you want, you could export the value of the url to see how it's wrong (or post the result to your question).如果这仍然不能满足您的要求,您可以导出url的值以查看其错误(或将结果发布到您的问题中)。

export('url', url)

This happens because the environment variables field of the lambda function needs a string.发生这种情况是因为 lambda function 的环境变量字段需要一个字符串。 We need to tell Pulumi to ensure that the bucket id has resolved (ie, the bucket is finish creating and the outputs have been returned)我们需要告诉Pulumi确保bucket id已经解析(即bucket创建完成并且输出已经返回)

If you pass the Output.concat to the lambda function like so, it works:如果像这样将Output.concat传递给 lambda function ,它可以工作:

lambda_func = aws.lambda_.Function(
    "function",
    runtime="python3.7",
    role=role.arn,
    handler="hello_world.handler",
    code=pulumi.AssetArchive({".": pulumi.FileArchive(".")}),
    environment=aws.lambda_.FunctionEnvironmentArgs(
        variables={"STORE_PATH": pulumi.Output.concat("s3://", bucket.id, "/")}
    ),
)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM