Haim Raman
Haim Raman

Reputation: 12043

Define lifecycle role on aws s3 bucket with terraform - ARN: arn: invalid prefix

I need to define a life cycle rule on S3 Bucket with transform.

I was following the configurations here. I used "aws_s3_bucket_lifecycle_configuration" as the docs says

The lifecycle_rule attribute is deprecated. See aws_s3_bucket_lifecycle_configuration for examples with object lifecycle rules.

It looks straightforward.

resource "aws_s3_bucket" "my-bucket" {
  bucket = "my-bucket"
}

resource "aws_s3_bucket_lifecycle_configuration" "my-bucket" {
  bucket = aws_s3_bucket.my-bucket.id

  rule {
    id = "rule-1"

    expiration {
      days = 31
    }
    status = "Enabled"
  }
}

Unfortunately this will result with

"bucket" (my-bucket) is an invalid ARN: arn: invalid prefix[0m

So I tried replacing id with arn as suggested here

resource "aws_s3_bucket_lifecycle_configuration" "my-bucket" {
  bucket = aws_s3_bucket.my-bucket.arn

  rule {
    id = "rule-1"

    expiration {
      days = 31
    }
    status = "Enabled"
  }
}

Again the same issue (I was expecting the .arn to actually render an arn)

Finally, I just went to the AWS Management console and copied the name

resource "aws_s3_bucket_lifecycle_configuration" "my-bucket" {
  bucket = "arn:aws:s3:::my-bucket"

  rule {
    id = "rule-1"

    expiration {
      days = 31
    }
    status = "Enabled"
  }
}

Now I get

Error: parsing S3 Control Bucket ARN (): unknown format

Can anyone provide a working example?

Note: I am using Terraform 1.3.6

Upvotes: 2

Views: 786

Answers (1)

Allan Chua
Allan Chua

Reputation: 10185

I'm not sure what version of Terraform CLI are you using but this sample works for me:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0"
    }
  }
}

provider "aws" {
  region                  = "ap-southeast-1"
  shared_credentials_file = "~/.aws/credentials" # Location of named profiles inside developer machine
  profile                 = "my-aws-profile" # Provide your AWS CLI named profile
}


resource "aws_s3_bucket" "main_s3" {
    bucket = "main-s3-for-tf-poc"
}

resource "aws_s3_bucket_lifecycle_configuration" "my_bucket_lc" {
  bucket = aws_s3_bucket.main_s3.id

  rule {
    id = "rule-1"

    expiration {
      days = 31
    }
    status = "Enabled"
  }
}

Terraform CLI --version output

terraform --version
Terraform v1.5.2
on darwin_arm64
+ provider registry.terraform.io/hashicorp/aws v3.76.1

Comment from the author of the question:

This answer is correct and worked for me with an older version of Terraform. The key difference between my code and the example is the usage of '-' instead of '_' so switch from

"aws_s3_bucket" "main-s3" 

to

"aws_s3_bucket" "main_s3" 

Upvotes: 1

Related Questions