-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fn::If intrinsic function inside Auth property of "AWS::Serverless::Api" #1859
Comments
Would love to see this added. Currently trying to configure an optional Authorizer for my prod stack but not my dev and unable to. |
This also seems to relate to my issue with the AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Test template
Parameters:
AddApiMapping:
Default: n
Type: String
AllowedValues: [y,n]
Description: 'y/N'
Conditions:
AddApiMappingCondition:
!Or [!Equals ['y', !Ref AddApiMapping], !Equals ['Y', !Ref AddApiMapping]]
Resources:
ApiGateway:
Type: AWS::Serverless::Api
Properties:
Name: TestApi
..
Domain:
!If
- AddApiMappingCondition
- CertificateArn: !Ref CertificateArn
DomainName: !Ref ApiMappingDomainName
- !Ref "AWS::NoValue" Running
|
Any updates on this? We ran into this issue as well. We're trying to deploy an API Gateway in the Beijing region where Cognito User Pools are not supported, and we're trying to add a condition in the
But the above fails with the following error:
The related line in the code where it fails is
|
Having same issue. Cannot make a resource policy conditional. |
Having the same issue. Is there a workaround? |
Off topic: Parameters:
CustomDomainName:
Type: String
Description: Domain name for api (e.g. my_service.<environment>.example.com)
Default: 'false'
ZoneId:
Type: AWS::SSM::Parameter::Value<String>
Description: Route53 zone ID from SSM Parameter Store.
Default: external-route53-zone-id
Conditions:
ApiCustomDomain: !Not
- !Equals
- 'false'
- !Ref CustomDomainName
Resources:
ApiSSLCertificate:
Type: AWS::CertificateManager::Certificate
Properties:
CertificateTransparencyLoggingPreference: DISABLED
DomainName: !Ref CustomDomainName
ValidationMethod: DNS
DomainValidationOptions:
DomainName: !Ref CustomDomainName
HostedZoneId: !Ref ZoneId
Condition: ApiCustomDomain
ApiCustomDomain:
Type: AWS::ApiGateway::DomainName
Properties:
DomainName: !Ref CustomDomainName
EndpointConfiguration:
Types:
- REGIONAL
RegionalCertificateArn: !Ref ApiSSLCertificate
SecurityPolicy: TLS_1_2
Condition: ApiCustomDomain
ApiCustomDomainRecord:
Type: AWS::Route53::RecordSet
Properties:
HostedZoneId: !Ref ZoneId
Name: !Ref CustomDomainName
ResourceRecords:
- !GetAtt ApiCustomDomain.RegionalDomainName
TTL: 900
Type: CNAME
Condition: ApiCustomDomain
ApiCustomDomainMapping:
Type: AWS::ApiGateway::BasePathMapping
Properties:
DomainName: !Ref ApiCustomDomain
RestApiId: !Ref Api
Stage: !Ref Api.Stage # This is a resource generated when the stack is deployed.
Condition: ApiCustomDomain
Api:
Type: AWS::Serverless::Api
Properties:
... # Don't specify `Domain:`. It's handled with `ApiCustomDomainMapping`.
|
When will this issue be addressed? Then, due to the issue that |
You might be able to get this to work by adding Transform:
- AWS::LanguageExtensions
- AWS::Serverless-2016-10-31
See #2533 for more information. |
Closing in favor of #2533. |
@hoffa Even with AWS::LanguageExtensions, it's not deployable because both SAM CLI and AWS detects that if is not supported |
Do you have a reproducible template? I did something similar in #214 (comment). |
I've tried implementing If to conditionally add (or not to add) Auth section for AWS::Serverless:API but even after adding this extra macro (obviously, before the Serverless) - I got an error that If is not a supported property for the resource. I'm currently writing a custom macro that is removing that Auth section from the template if it detects a certain environment, but native intrinsic functions support would be great to see in the near future :) |
I tried by creating the following template Transform:
- AWS::LanguageExtensions
- AWS::Serverless-2016-10-31
Parameters:
WithAuth:
Type: String
Conditions:
WithAuthCondition: !Equals [!Ref WithAuth, "yes"]
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.8
Handler: foo
InlineCode: bar
Events:
ApiEvent:
Type: Api
Properties:
Method: get
Path: /
RestApiId: !Ref MyApi
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
Auth:
Authorizers:
Fn::If:
- WithAuthCondition
- LambdaRequestAuthorizer:
FunctionArn: !GetAtt MyFunction.Arn
- !Ref AWS::NoValue Then deploying without authorizer:
And with authorizer:
And it seems to work as expected. |
Wow, Thank you @hoffa it's working as expected!!! |
Description:
In the
AWS::Serverless::Api
resource I want to specify optional authorizer. For this purpose I try to use Conditions and "!If" intrinsic function. SAM doees not consider such a template as a valid one. I tried already all combinations with "!If" and "Fn::If" and indentation - it does not work.Steps to reproduce the issue:
sam validate
Observed result:
Expected result:
Template is considered as valid and can be deployed with optional parameter
ExternalAuthorizerArn
set to either empty string or to the specific value.The text was updated successfully, but these errors were encountered: