AWS::Serverless::WebFunction
Creates an AWS Lambda Web Function, which serves HTTP requests through a managed endpoint. Unlike
a Lambda Event Function, which runs in response to invocations and events, a Web Function runs
your web server and receives HTTP requests directly. From a single
AWS::Serverless::WebFunction, AWS SAM generates the Web Function, an immutable revision
that contains your code and configuration, and an endpoint that routes traffic to one or more
revisions.
Note
When you deploy to AWS CloudFormation, AWS SAM transforms your AWS SAM resources into CloudFormation resources. For more information, see Generated CloudFormation resources for AWS SAM.
Important
Your function code must run an HTTP server that listens on the host and port in the
AWS_LAMBDA_HTTP_ENDPOINT environment variable. The runtime does not set
PORT. By default, the runtime runs index.js at the root of the
deployment package. Use EntryPoint to run a
different file.
Important
The Amazon S3 bucket that holds your deployment package must meet the following requirements:
-
Versioning is enabled.
-
A bucket policy grants
lambda.amazonaws.comthes3:GetObjectands3:GetObjectVersionpermissions. We recommend that you scope the policy with anArnLikecondition onaws:SourceArnthat matches the function's revisions, for examplearn:aws:lambda:.us-east-1:111122223333:web-function/my-function/revision/* -
If the object is encrypted with SSE-KMS, it uses a customer managed key whose key policy allows
lambda.amazonaws.comto callkms:Decrypt. The AWS managed key for Amazon S3 (aws/s3) isn't supported, because you can't change its key policy.
For an example bucket policy, see Bucket policy for Lambda in the AWS Lambda Developer Guide.
CodeUri must reference an object in Amazon S3. The sam build and
sam package commands don't support local paths for this resource.
Syntax
To declare this entity in your AWS Serverless Application Model (AWS SAM) template, use the following syntax.
YAML
Type: AWS::Serverless::WebFunction Properties: AuthType:StringAutoDeploymentMode:StringCodeUri:String | CodeUriEgressNetworkConnectorArn:StringEndpointDescription:StringEndpointName:StringEndpointType:StringEntryPoint:StringEnvironmentVariables:MapExecutionRoleArn:StringFunctionName:StringInlineCode:StringKmsKeyArn:StringLoggingConfig:MapMaxConcurrencyPerEnvironment:IntegerReplicaRegions:List | ReplicaRegionsRevisionDescription:StringRevisionWeights:RevisionWeightsRuntime:StringScalingConfig:MapTags:MapThrottleConfig:MapTimeout:Integer
Properties
-
AuthType -
How the endpoint authorizes requests.
-
IamAuth: Lambda authorizes requests with Signature Version 4 and IAM. Callers need thelambda:InvokeWebFunctionEndpointpermission, granted through a resource-based policy on the function. -
ApplicationManaged: the endpoint is public. Your function must authorize every request itself.
Valid values:
IamAuth,ApplicationManagedType: String
Required: Yes
CloudFormation compatibility: This property is passed directly to the
AuthTypeproperty of anAWS::Lambda::WebFunctionEndpointresource. -
-
AutoDeploymentMode -
How the endpoint routes traffic across revisions.
-
LatestRevision: sends 100% of traffic to the revision that this deployment creates. -
Disabled: routes traffic according to RevisionWeights.
Which values you can use, and whether
RevisionWeightsis required, depends on EndpointType:-
HomeRegion: use either value. WithDisabled,RevisionWeightsis required. -
MultiRegionorPerRegion: useDisabled. If you omitRevisionWeights, the endpoint routes 100% of traffic to the revision created in the same stack, so you can create the endpoint in a single deployment. Traffic doesn't move to revisions that later deployments create.
The value must resolve when AWS SAM transforms the template. A
Refto a parameter is supported. Other intrinsic functions are not supported.Valid values:
LatestRevision,DisabledType: String
Required: No
Default:
LatestRevisionCloudFormation compatibility: This property is unique to AWS SAM and doesn't have an CloudFormation equivalent.
-
-
CodeUri -
The Amazon S3 location of the deployment package. Specify an
s3://URI or an object withbucket/keyBucket,Key, and optionalVersionkeys. For bucket requirements, see the note at the top of this page.Type: String | CodeUri
Required: Yes
CloudFormation compatibility: This property is similar to the
BuildConfig.CodeConfig.S3Objectproperty of anAWS::Lambda::WebFunctionRevisionresource. AWS SAM converts ans3://URI intoBucket,Key, andVersionId. -
EgressNetworkConnectorArn -
The version-qualified ARN of a network connector that routes the function's outbound traffic through your VPC. The ARN must end in a version number, for example
arn:aws:lambda:us-east-1:123456789012:network-connector:my-connector:1. To use the latest version of a connector in the same template, specify!GetAtt. The connector's ComputeTypes must beMyConnector.LatestVersionArnWebFunction.If you specify a literal string, AWS SAM checks that it's a version-qualified network connector ARN. Intrinsic functions are passed through and validated by the service. Changing this value creates a new revision.
Type: String
Required: No
CloudFormation compatibility: This property is passed directly to the
ServiceConfig.EgressNetworkConnectorArnproperty of anAWS::Lambda::WebFunctionRevisionresource. -
EndpointDescription -
A description of the endpoint.
Type: String
Required: No
CloudFormation compatibility: This property is passed directly to the
Descriptionproperty of anAWS::Lambda::WebFunctionEndpointresource. -
EndpointName -
The name of the endpoint. Changing the name replaces the endpoint. Changing
EndpointTypealso replaces the endpoint, so changeEndpointNameat the same time. CloudFormation can't replace a custom-named resource without a new name.Type: String
Required: No
Default:
prodCloudFormation compatibility: This property is passed directly to the
EndpointNameproperty of anAWS::Lambda::WebFunctionEndpointresource. -
EndpointType -
Where the endpoint serves traffic.
-
HomeRegion: serves from the stack's Region. -
MultiRegion: replicates the function to the Regions in ReplicaRegions and routes each request to the nearest Region. -
PerRegion: exposes a separate endpoint in each replica Region.
MultiRegionandPerRegionrequireAutoDeploymentMode: Disabled.Valid values:
HomeRegion,MultiRegion,PerRegionType: String
Required: No
Default:
HomeRegionCloudFormation compatibility: This property is passed directly to the
EndpointTypeproperty of anAWS::Lambda::WebFunctionEndpointresource. -
-
EntryPoint -
The file that the runtime runs, relative to the root of the deployment package. AWS SAM passes it to the runtime in the reserved
AWS_LAMBDA_NODEJS_ENTRYPOINTenvironment variable.You can't use this property when
EnvironmentVariablesis an intrinsic function.Type: String
Required: No
Default:
index.jsCloudFormation compatibility: This property is unique to AWS SAM and doesn't have an CloudFormation equivalent.
-
EnvironmentVariables -
Key-value pairs to set as environment variables for the revision.
-
Keys must start with a letter and contain only letters, numbers, and underscores.
-
Values must be non-empty and at most 4,096 characters.
-
AWS SAM converts number and Boolean values to strings.
Type: Map
Required: No
CloudFormation compatibility: This property is passed directly to the
ServiceConfig.EnvironmentVariablesproperty of anAWS::Lambda::WebFunctionRevisionresource. -
-
ExecutionRoleArn -
The ARN of the IAM role that the function assumes when it runs. If you don't specify a role, AWS SAM creates one with the
AWSLambdaBasicExecutionRolemanaged policy and a logical ID of.<resource-logical-id>RoleType: String
Required: No
CloudFormation compatibility: This property is passed directly to the
ServiceConfig.ExecutionRoleArnproperty of anAWS::Lambda::WebFunctionRevisionresource. -
FunctionName -
The name of the function. Function names must be unique within an account across all Regions. If you don't specify a name, AWS SAM generates one from the logical ID and the stack ID, at most 64 characters. Changing the name replaces the function, its revision, and its endpoint.
Type: String
Required: No
CloudFormation compatibility: This property is passed directly to the
FunctionNameproperty of theAWS::Lambda::WebFunction,AWS::Lambda::WebFunctionRevision, andAWS::Lambda::WebFunctionEndpointresources. -
InlineCode -
Not supported for
AWS::Serverless::WebFunction. Use CodeUri.Type: String
Required: No
CloudFormation compatibility: This property is unique to AWS SAM and doesn't have an CloudFormation equivalent.
-
KmsKeyArn -
The ARN of the AWS KMS key used to encrypt the revision's environment variables.
Type: String
Required: No
CloudFormation compatibility: This property is passed directly to the
KmsKeyArnproperty of anAWS::Lambda::WebFunctionRevisionresource. -
LoggingConfig -
The logging configuration for the revision, such as the log group and log levels.
Type: Map
Required: No
CloudFormation compatibility: This property is passed directly to the
ServiceConfig.TelemetryConfig.LoggingConfigproperty of anAWS::Lambda::WebFunctionRevisionresource. -
MaxConcurrencyPerEnvironment -
The maximum number of concurrent requests that each execution environment handles.
Type: Integer
Required: No
CloudFormation compatibility: This property is passed directly to the
ServiceConfig.MaxConcurrencyPerEnvironmentproperty of anAWS::Lambda::WebFunctionRevisionresource. -
ReplicaRegions -
The Regions to replicate the function to. Use this property with
MultiRegionandPerRegionendpoints. Specify a list of Region codes and Region groups, or an object withIncludeRegionsand optionalExcludeRegions.Type: List | ReplicaRegions
Required: No
CloudFormation compatibility: This property is similar to the
Regionsproperty of anAWS::Lambda::WebFunctionEndpointresource. AWS SAM expands Region groups and appliesExcludeRegionsto produce the Region list. -
RevisionDescription -
A description of the revision that this deployment creates.
Type: String
Required: No
CloudFormation compatibility: This property is passed directly to the
Descriptionproperty of anAWS::Lambda::WebFunctionRevisionresource. -
RevisionWeights -
A map of revision IDs to traffic weights. Use this property when
AutoDeploymentModeisDisabled. Specify up to two revisions. Each weight is an integer from 1 to 100, and the weights must add up to 100.Type: RevisionWeights
Required: Conditional. Required when
AutoDeploymentModeisDisabledandEndpointTypeisHomeRegion.CloudFormation compatibility: This property is similar to the
RevisionWeightsproperty of anAWS::Lambda::WebFunctionEndpointresource. AWS SAM converts the map into a list ofRevisionIdandWeightobjects. -
Runtime -
The runtime for the function, for example
nodejs24.x.Type: String
Required: Yes
CloudFormation compatibility: This property is passed directly to the
BuildConfig.RuntimeConfig.Runtimeproperty of anAWS::Lambda::WebFunctionRevisionresource. -
ScalingConfig -
The scaling configuration for the endpoint, such as the maximum number of execution environments.
Type: Map
Required: No
CloudFormation compatibility: This property is passed directly to the
ScalingConfigproperty of anAWS::Lambda::WebFunctionEndpointresource. -
A map of key-value pairs that specifies the tags added to the function and to the execution role that AWS SAM generates.
Type: Map
Required: No
CloudFormation compatibility: This property is similar to the
Tagsproperty of anAWS::Lambda::WebFunctionresource. TheTagsproperty in AWS SAM consists of key-value pairs (whereas in CloudFormation this property consists of a list ofTagobjects). Also, AWS SAM automatically adds alambda:createdBy:SAMtag to the function and to the default role that is generated for it. -
ThrottleConfig -
The throttling configuration for the endpoint, such as the maximum request rate.
Type: Map
Required: No
CloudFormation compatibility: This property is passed directly to the
ThrottleConfigproperty of anAWS::Lambda::WebFunctionEndpointresource. -
Timeout -
The maximum time, in seconds, that the function can run per request. Valid values are from 3 to 900.
Type: Integer
Required: No
Default:
30CloudFormation compatibility: This property is passed directly to the
ServiceConfig.TimeoutSecondsproperty of anAWS::Lambda::WebFunctionRevisionresource.
Return Values
Ref
When the logical ID of this resource is provided to the Ref intrinsic
function, it returns the ARN of the Web Function. Where you need the function name, use the
value of FunctionName instead.
For more information about using the Ref function, see Ref in the AWS CloudFormation User Guide.
Fn::GetAtt
Fn::GetAtt returns a value for a specified attribute of this type. The
following are the available attributes and sample return values.
For more information about using Fn::GetAtt, see Fn::GetAtt in the AWS CloudFormation User Guide.
FunctionArn-
The Amazon Resource Name (ARN) of the Web Function.
State-
The current state of the Web Function.
StateReason-
The reason for the current state of the Web Function.
CreatedAt-
The time that the Web Function was created.
UpdatedAt-
The time that the Web Function was last updated.
To reference the revision or endpoint that AWS SAM generates, use their logical IDs,
and
<resource-logical-id>Revision. For example,
<resource-logical-id>Endpoint!GetAtt MyWebFunctionEndpoint.DomainName returns the domain name of the
endpoint for a Web Function with the logical ID MyWebFunction. For more
information, see CloudFormation resources generated when AWS::Serverless::WebFunction is specified.
Examples
Basic Web Function
The following example creates a Web Function with an IAM-authorized endpoint in the stack's Region. AWS SAM generates the execution role, and the endpoint routes all traffic to the revision that this deployment creates.
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Resources: MyWebFunction: Type: AWS::Serverless::WebFunction Properties: Runtime: nodejs24.x AuthType: IamAuth CodeUri: Bucket: amzn-s3-demo-bucket Key: webfn.zip Version:object-version-idTimeout: 30 Outputs: EndpointUrl: Value: !Sub https://${MyWebFunctionEndpoint.DomainName}
Split traffic between two revisions
The following example disables automatic deployment and sends 90% of traffic to one existing revision and 10% to another. Both revisions must already exist.
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Resources: MyWebFunction: Type: AWS::Serverless::WebFunction Properties: Runtime: nodejs24.x AuthType: IamAuth CodeUri: s3://amzn-s3-demo-bucket/webfn-v2.zip AutoDeploymentMode: Disabled RevisionWeights: rev-abc123: 90 rev-def456: 10
Multi-Region Web Function
The following example replicates the function to the US Regions except
us-west-1 and routes each request to the nearest Region. Because
RevisionWeights is omitted, the endpoint routes all traffic to the revision
created in this stack.
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Resources: MyWebFunction: Type: AWS::Serverless::WebFunction Properties: Runtime: nodejs24.x AuthType: IamAuth CodeUri: Bucket: amzn-s3-demo-bucket Key: webfn.zip Version:object-version-idEndpointName: prodmr EndpointType: MultiRegion AutoDeploymentMode: Disabled ReplicaRegions: IncludeRegions: - US ExcludeRegions: - us-west-1
Web Function with VPC egress
The following example creates a network connector for Web Functions and routes the function's outbound traffic through the connector's subnets and security groups.
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Resources: MyConnector: Type: AWS::Serverless::NetworkConnector Properties: ComputeTypes: - WebFunction VpcConfig: SubnetIds: - subnet-0abc123def4567890 - subnet-0def456abc7890123 SecurityGroupIds: - sg-0abc1234abcde0001 NetworkProtocol: IPv4 MyWebFunction: Type: AWS::Serverless::WebFunction Properties: Runtime: nodejs24.x AuthType: IamAuth CodeUri: Bucket: amzn-s3-demo-bucket Key: webfn.zip Version:object-version-idEgressNetworkConnectorArn: !GetAtt MyConnector.LatestVersionArn