Bug: Unable to resolve env var when referencing FunctionUrl
- Dominant language
- Python
- Stars
- 6.7k
- Forks
- 1.2k
- Avg merge
- 1d 10h
- Merged PRs (30d)
- 52
Description
### Description:
I am creating two functions in my template. `Function1` is conditionally referencing a FunctionUrl which is created in `Function2`. If the condition is true then I set `!GetAtt Function2Url.FunctionUrl` as the value of an environment variable called `FUNCTION_URL`. If the condition is false I reference a parameter as the value of the environment variable.
The problem is that even if the condition is false I get `Unable to resolve property FUNCTION_URL` and the env var is an empty string in the function code. However, if I remove the condition and directly reference the parameter, it works.
It also works (with the condition) if I deploy the stack and run the lambda in AWS.
### Steps to reproduce:
template.yaml:
```yaml
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Parameters:
EnvironmentName:
Type: String
MyCustomUrl:
Type: String
Conditions:
IsLocal: !Equals [!Ref EnvironmentName, local]
Resources:
Function1:
Type: AWS::Serverless::Function
Properties:
CodeUri: .
Handler: Function1.handler
Runtime: nodejs16.x
Environment:
Variables:
FUNCTION_URL: !If
- IsLocal
- !Ref MyCustomUrl
# SAM automatically creates an AWS::Lambda::Url resource under the hood when creating Function2
- !GetAtt Function2Url.FunctionUrl
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: false
Target: "es2020"
Sourcemap: true
EntryPoints:
- Function1.ts
Function2:
Type: AWS::Serverless::Function
Properties:
CodeUri: .
Handler: Function2.handler
Runtime: nodejs16.x
FunctionUrlConfig:
AuthType: NONE
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: false
Target: "es2020"
Sourcemap: true
EntryPoints:
- Function2.ts
```
Function1 code:
```typescript
export const handler = async () => {
const { FUNCTION_URL } = process.env
if (!FUNCTION_URL) {
console.error("Missing FUNCTION_URL. Got:", FUNCTION_URL)
} else {
console.log(FUNCTION_URL)
}
return {
statusCode: 200,
body: JSON.stringify({message: "hello from Function1"})
};
};
```
samconfig.toml:
```toml
[default.local_invoke.parameters]
profile = "my-profile"
parameter_overrides = "EnvironmentName=\"local\" MyCustomUrl=\"www.github.com\""
```
### Observed result:
If I run this with `sam build && sam local invoke Function1 --debug` I get:
2022-12-22 17:03:31,033 | Unable to resolve property FUNCTION_URL: OrderedDict([('Fn::If', ['IsLocal', OrderedDict([('Ref', 'MyCustomUrl')]), OrderedDict([('Fn::GetAtt', ['Function2Url', 'FunctionUrl'])])])]). Leaving as is.
...
2022-12-22T16:03:32.317Z 70045c0d-f199-4262-884b-0b3268a4685d ERROR Missing FUNCTION_URL. Got:
### Expected result:
If I remove the condition in the template, i.e.:
```yaml
Environment:
Variables:
FUNCTION_URL: !Ref MyCustomUrl
```
I get the expected output: `www.github.com`
As said, this only happens with `sam local invoke`, not when running the lambda in AWS.
### Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
1. OS: macOS 13.0.1
2. `sam --version`: SAM CLI, version 1.67.0
3. AWS region: eu-west-1
`Add --debug flag to command you are running`
Contributor guide
Assessment
This issue has not been assessed yet.