Data Source: aws_s3_bucket_object

The S3 object data source allows access to the metadata and _optionally_ (see below) content of an object stored inside S3 bucket.

Example Usage

The following example retrieves a text object (which must have a Content-Type value starting with text/) and uses it as the user_data for an EC2 instance:

data "aws_s3_bucket_object" "bootstrap_script" {
  bucket = "ourcorp-deploy-config"
  key    = "ec2-bootstrap-script.sh"
}

resource "aws_instance" "example" {
  instance_type = "t2.micro"
  ami           = "ami-2757f631"
  user_data     = data.aws_s3_bucket_object.bootstrap_script.body
}

The following, more-complex example retrieves only the metadata for a zip file stored in S3, which is then used to pass the most recent version_id to AWS Lambda for use as a function implementation. More information about Lambda functions is available in the documentation for aws_lambda_function.

data "aws_s3_bucket_object" "lambda" {
  bucket = "ourcorp-lambda-functions"
  key    = "hello-world.zip"
}

resource "aws_lambda_function" "test_lambda" {
  s3_bucket         = data.aws_s3_bucket_object.lambda.id
  s3_key            = data.aws_s3_bucket_object.lambda.key
  s3_object_version = data.aws_s3_bucket_object.lambda.version_id
  function_name     = "lambda_function_name"
  role              = aws_iam_role.iam_for_lambda.arn # (not shown)
  handler           = "exports.test"
}

Argument Reference

This data source supports the following arguments:

Attribute Reference

This data source exports the following attributes in addition to the arguments above: