aws / aws/serverless-application-model

Unsupported intrinsic functions

Open
#2,533 27 comments 15 reactions 0 assignees View on GitHub
area/intrinsics meta
Dominant language
Python
Stars
9.6k
Forks
2.5k
Avg merge
1d 11h
Merged PRs (30d)
7

Description

## The problem

Some properties have limited support for [intrinsic functions](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html) (e.g. `Ref`, `Fn::GetAtt`, `Fn::If`, etc.).

Using unsupported intrinsic functions in such properties can cause issues with deployment.

## Why it happens

AWS SAM is a [AWS CloudFormation macro](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-macros.html); it receives a [SAM template](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-template-anatomy.html) as input (as-is, along with the intrinsic functions), and returns a [CloudFormation template](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-anatomy.html) which is then deployed by CloudFormation.

This means that SAM is unable to resolve some intrinsic functions. For example, an `!If` condition with a `Ref` to a [stack parameter](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/parameters-section-structure.html) is resolvable (SAM has access to the stack parameters), but a `Ref` to a stack resource is not (as the transform happens before deployment). Furthermore, SAM supports limited intrinsic function resolution for only some properties.

It's not an issue for properties that are passed as-is to properties of the underlying CloudFormation resources, but it becomes an issue when SAM must know the value for its transform logic.

## Workarounds

### Add the `AWS::LanguageExtensions` transform

The [`AWS::LanguageExtensions` transform](https://aws.amazon.com/blogs/mt/introducing-new-language-extensions-in-aws-cloudformation/) will resolve intrinsic functions if the value is known when [`Transform`s](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-macros.html) are run.

Replace:

```yaml
Transform: AWS::Serverless-2016-10-31
```

With:

```yaml
Transform:
- AWS::LanguageExtensions
- AWS::Serverless-2016-10-31
```

The [AWS SAM CLI](https://github.com/aws/aws-sam-cli) currently doesn't process `AWS::LanguageExtensions` locally, so it won't work where local transforms are needed.

> [!NOTE]
> There is a known issue where `AWS::Serverless-2016-10-31` and `AWS::LanguageExtensions` can conflict; see https://github.com/aws-cloudformation/cfn-language-discussion/issues/109.

### Use a pass-through property, if available

Pass-through properties are passed directly to the underlying CloudFormation resources, hence intrinsic functions work. Check the "AWS CloudFormation compatibility" notice under properties to see whether the property is passed as-is to an underlying CloudFormation resource.

For example for the `Schedule` event type, use the [`State` property](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-schedule.html#sam-function-schedule-state) instead of [`Enabled`](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-schedule.html#sam-function-schedule-enabled).

### Use raw CloudFormation

If nothing else works, you can always switch to using the underlying CloudFormation resources directly. Since they are not processed by SAM, CloudFormation will be able to resolve the intrinsic functions.

You can get the transformed CloudFormation template of a stack `` using:

```bash
aws cloudformation get-template --query TemplateBody --change-set-name "$(aws cloudformation describe-stacks --query 'Stacks[0].ChangeSetId' --output text --stack-name )"
```

Which you can then use to replace the affected resources.

See also https://github.com/aws/serverless-application-model/issues/3007#issuecomment-1464194429 for other ways of transforming a SAM template into a CloudFormation template.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.