如何从Terraform的cloudfront-w-s3模块中提取S3 bucket及ARN作为输出
获取CloudFront关联S3桶信息的输出方法
- 首先确认你使用的
/modules/cloudfront-w-s3/aws模块已暴露S3相关输出属性
这类通用模块通常默认会声明s3_bucket_name(桶名称)、s3_bucket_arn(桶ARN)两个输出值,你可以执行以下命令查看该模块所有已公开的输出,确认属性名:
如果模块未声明对应输出,你需要修改模块源码下的terraform output -module=cloudfrontoutputs.tf文件,添加如下内容:output "s3_bucket_name" { description = "Name of the S3 bucket bound to CloudFront distribution" value = aws_s3_bucket.this.name # 此处aws_s3_bucket.this为模块内定义S3桶的资源名,根据实际情况调整 } output "s3_bucket_arn" { description = "ARN of the S3 bucket bound to CloudFront distribution" value = aws_s3_bucket.this.arn # 此处aws_s3_bucket.this为模块内定义S3桶的资源名,根据实际情况调整 } - 然后在你的根Terraform配置中添加输出声明
按照module.<模块名>.<输出属性名>的格式引用模块返回值即可,示例如下:output "cloudfront_s3_bucket_name" { description = "Name of S3 bucket used with CloudFront" value = module.cloudfront.s3_bucket_name } output "cloudfront_s3_bucket_arn" { description = "ARN of S3 bucket used with CloudFront" value = module.cloudfront.s3_bucket_arn }
配置完成后执行terraform apply,执行完成后终端就会打印对应的输出值,你也可以后续执行terraform output随时查看。
内容的提问来源于stack exchange,提问作者setup
相关产品推荐
相关产品推荐

