Skip to content

Resource handler returned message: "Cannot invoke "String.hashCode()" because "<local4>" is null" (RequestToken: ..., HandlerErrorCode: InternalFailure)

0

Hello I am having an issue when creating a simple bucket with Cloudfomation. The template is simple, I cannot figure out the root cause of this issue

AWSTemplateFormatVersion: 2010-09-09

Parameters:
  EnvironmentName:
    Type: String
    Default: dev
    Description: Refer the current environment
    AllowedValues: ['dev', 'prod', 'preprod']
    ConstraintDescription: Select 'dev' or 'prod' or 'preprod'

Resources:
  DigidocsGeneralBucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: somebucket-${EnvironmentName}
      Tags:
      - Key: Application
        Value: someapplication

asked 2 years ago1.8K views

2 Answers
2
Accepted Answer

Hi,

your BucketName: somebucket-${EnvironmentName} is incomplete. You must change it to BucketName: !Sub somebucket-${EnvironmentName} to allow substitution of ${EnvironmentName} to happen.

See !Sub at https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html

Best,

Didier

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

1

The error message you're encountering suggests that there's an issue with the CloudFormation template, specifically related to string processing. The problem likely stems from how you're trying to construct the bucket name.

In your template, you're using a parameter substitution syntax that isn't correct for CloudFormation. CloudFormation doesn't support the ${EnvironmentName} syntax directly in the BucketName property. Instead, you should use the Fn::Sub intrinsic function (or its shorthand !Sub) to perform string substitution.

Here's how you can modify your template to correctly use the EnvironmentName parameter in the BucketName:

AWSTemplateFormatVersion: 2010-09-09

Parameters:
  EnvironmentName:
    Type: String
    Default: dev
    Description: Refer the current environment
    AllowedValues: ['dev', 'prod', 'preprod']
    ConstraintDescription: Select 'dev' or 'prod' or 'preprod'

Resources:
  DigidocsGeneralBucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: !Sub 'somebucket-${EnvironmentName}'
      Tags:
      - Key: Application
        Value: someapplication

By using the !Sub function, you're telling CloudFormation to substitute the ${EnvironmentName} with the actual value of the EnvironmentName parameter.

This should resolve the "Cannot invoke "String.hashCode()" because "<local4>" is null" error you're experiencing. The error was likely caused by CloudFormation trying to process an invalid string substitution, resulting in a null value being passed to an internal method.

Remember that S3 bucket names must be globally unique across all AWS accounts, so you might want to add some additional uniqueness to your bucket name to avoid conflicts, especially if you're creating multiple environments.
Sources
Resource handler returned message: "<resourceName> already exists | AWS re:Post
Access Denied (Service: S3, Status Code: 403) | AWS re:Post

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.