@ashishpandeyone/cdk-lambda-web-function 0.1.7 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.jsii +81 -107
- package/.jsii.tabl.json +1 -1
- package/API.md +21 -18
- package/README.md +38 -3
- package/assets/web-function-provider/index.js +23 -23
- package/lib/nodejs-web-function.js +1 -1
- package/lib/private/code-bucket.js +3 -1
- package/lib/private/nag.d.ts +19 -0
- package/lib/private/nag.js +139 -0
- package/lib/private/provider-protocol.d.ts +15 -0
- package/lib/private/provider-protocol.js +7 -2
- package/lib/private/resource-policy-statements.d.ts +23 -0
- package/lib/private/resource-policy-statements.js +112 -0
- package/lib/private/traffic-shift.js +10 -6
- package/lib/web-function-deployment.js +1 -1
- package/lib/web-function-endpoint.js +1 -1
- package/lib/web-function-grants.d.ts +3 -3
- package/lib/web-function-grants.js +8 -9
- package/lib/web-function-origin.d.ts +1 -1
- package/lib/web-function-origin.js +7 -9
- package/lib/web-function-revision.js +1 -1
- package/lib/web-function.d.ts +25 -18
- package/lib/web-function.js +62 -49
- package/package.json +1 -1
package/API.md
CHANGED
|
@@ -145,9 +145,9 @@ public addToResourcePolicy(statement: PolicyStatement): AddToResourcePolicyResul
|
|
|
145
145
|
|
|
146
146
|
Adds a statement to the function's resource policy, e.g. to let another account manage the function. For invoking, prefer `addPermission` or `grantInvoke`.
|
|
147
147
|
|
|
148
|
-
The function has
|
|
149
|
-
|
|
150
|
-
|
|
148
|
+
The function has a single resource policy. Each stack writes only its own statements into it, through a custom
|
|
149
|
+
resource that merges them with every other stack's, and with statements added outside CloudFormation. From a
|
|
150
|
+
stack in another account than the function, nothing is added.
|
|
151
151
|
|
|
152
152
|
###### `statement`<sup>Required</sup> <a name="statement" id="@ashishpandeyone/cdk-lambda-web-function.NodejsWebFunction.addToResourcePolicy.parameter.statement"></a>
|
|
153
153
|
|
|
@@ -851,9 +851,9 @@ public addToResourcePolicy(statement: PolicyStatement): AddToResourcePolicyResul
|
|
|
851
851
|
|
|
852
852
|
Adds a statement to the function's resource policy, e.g. to let another account manage the function. For invoking, prefer `addPermission` or `grantInvoke`.
|
|
853
853
|
|
|
854
|
-
The function has
|
|
855
|
-
|
|
856
|
-
|
|
854
|
+
The function has a single resource policy. Each stack writes only its own statements into it, through a custom
|
|
855
|
+
resource that merges them with every other stack's, and with statements added outside CloudFormation. From a
|
|
856
|
+
stack in another account than the function, nothing is added.
|
|
857
857
|
|
|
858
858
|
###### `statement`<sup>Required</sup> <a name="statement" id="@ashishpandeyone/cdk-lambda-web-function.WebFunction.addToResourcePolicy.parameter.statement"></a>
|
|
859
859
|
|
|
@@ -1457,7 +1457,7 @@ new WebFunctionBase(scope: Construct, id: string, props?: ResourceProps)
|
|
|
1457
1457
|
| <code><a href="#@ashishpandeyone/cdk-lambda-web-function.WebFunctionBase.with">with</a></code> | Applies one or more mixins to this construct. |
|
|
1458
1458
|
| <code><a href="#@ashishpandeyone/cdk-lambda-web-function.WebFunctionBase.applyCrossStackReferenceStrength">applyCrossStackReferenceStrength</a></code> | Override the cross-stack reference strength for this resource. |
|
|
1459
1459
|
| <code><a href="#@ashishpandeyone/cdk-lambda-web-function.WebFunctionBase.applyRemovalPolicy">applyRemovalPolicy</a></code> | Apply the given removal policy to this resource. |
|
|
1460
|
-
| <code><a href="#@ashishpandeyone/cdk-lambda-web-function.WebFunctionBase.addToResourcePolicy">addToResourcePolicy</a></code> | Adds a statement to the function's resource policy
|
|
1460
|
+
| <code><a href="#@ashishpandeyone/cdk-lambda-web-function.WebFunctionBase.addToResourcePolicy">addToResourcePolicy</a></code> | Adds a statement to the function's resource policy, e.g. to let another account manage the function. For invoking, prefer `addPermission` or `grantInvoke`. |
|
|
1461
1461
|
| <code><a href="#@ashishpandeyone/cdk-lambda-web-function.WebFunctionBase.grantInvoke">grantInvoke</a></code> | Allows `grantee` to call the function's endpoint. |
|
|
1462
1462
|
| <code><a href="#@ashishpandeyone/cdk-lambda-web-function.WebFunctionBase.metric">metric</a></code> | A metric of this function's execution environments in `AWS/Lambda/Web` (dimension `FunctionName`), across all endpoints. |
|
|
1463
1463
|
| <code><a href="#@ashishpandeyone/cdk-lambda-web-function.WebFunctionBase.metricActiveTime">metricActiveTime</a></code> | Time the execution environments were active (`EnvironmentActiveTime`). |
|
|
@@ -1545,7 +1545,11 @@ account for data recovery and cleanup later (`RemovalPolicy.RETAIN`).
|
|
|
1545
1545
|
public addToResourcePolicy(statement: PolicyStatement): AddToResourcePolicyResult
|
|
1546
1546
|
```
|
|
1547
1547
|
|
|
1548
|
-
Adds a statement to the function's resource policy
|
|
1548
|
+
Adds a statement to the function's resource policy, e.g. to let another account manage the function. For invoking, prefer `addPermission` or `grantInvoke`.
|
|
1549
|
+
|
|
1550
|
+
The function has a single resource policy. Each stack writes only its own statements into it, through a custom
|
|
1551
|
+
resource that merges them with every other stack's, and with statements added outside CloudFormation. From a
|
|
1552
|
+
stack in another account than the function, nothing is added.
|
|
1549
1553
|
|
|
1550
1554
|
###### `statement`<sup>Required</sup> <a name="statement" id="@ashishpandeyone/cdk-lambda-web-function.WebFunctionBase.addToResourcePolicy.parameter.statement"></a>
|
|
1551
1555
|
|
|
@@ -3214,7 +3218,7 @@ const webFunctionAttributes: WebFunctionAttributes = { ... }
|
|
|
3214
3218
|
| **Name** | **Type** | **Description** |
|
|
3215
3219
|
| --- | --- | --- |
|
|
3216
3220
|
| <code><a href="#@ashishpandeyone/cdk-lambda-web-function.WebFunctionAttributes.property.endpointName">endpointName</a></code> | <code>string</code> | The endpoint `grantInvoke` allows calling. |
|
|
3217
|
-
| <code><a href="#@ashishpandeyone/cdk-lambda-web-function.WebFunctionAttributes.property.webFunctionArn">webFunctionArn</a></code> | <code>string</code> | The function's ARN, e.g. `arn:aws:lambda:us-east-1:111122223333:web-function/orders`. It may be in another region or account; across accounts, `grantInvoke` also needs the function's resource policy to allow the caller. |
|
|
3221
|
+
| <code><a href="#@ashishpandeyone/cdk-lambda-web-function.WebFunctionAttributes.property.webFunctionArn">webFunctionArn</a></code> | <code>string</code> | The function's ARN, e.g. `arn:aws:lambda:us-east-1:111122223333:web-function/orders`. It may be in another region or account; across accounts, `grantInvoke` also needs the function's resource policy to allow the caller. A token (e.g. from another stack) is taken to be in this stack's account and region. |
|
|
3218
3222
|
| <code><a href="#@ashishpandeyone/cdk-lambda-web-function.WebFunctionAttributes.property.webFunctionName">webFunctionName</a></code> | <code>string</code> | The function's name. |
|
|
3219
3223
|
|
|
3220
3224
|
---
|
|
@@ -3241,7 +3245,7 @@ public readonly webFunctionArn: string;
|
|
|
3241
3245
|
- *Type:* string
|
|
3242
3246
|
- *Default:* built from `webFunctionName`, in this stack's account and region
|
|
3243
3247
|
|
|
3244
|
-
The function's ARN, e.g. `arn:aws:lambda:us-east-1:111122223333:web-function/orders`. It may be in another region or account; across accounts, `grantInvoke` also needs the function's resource policy to allow the caller.
|
|
3248
|
+
The function's ARN, e.g. `arn:aws:lambda:us-east-1:111122223333:web-function/orders`. It may be in another region or account; across accounts, `grantInvoke` also needs the function's resource policy to allow the caller. A token (e.g. from another stack) is taken to be in this stack's account and region.
|
|
3245
3249
|
|
|
3246
3250
|
---
|
|
3247
3251
|
|
|
@@ -4846,7 +4850,7 @@ The grant is on the endpoint's home-region ARN, which IAM evaluates for every re
|
|
|
4846
4850
|
endpoint serves from, including a MULTI_REGION global URL.
|
|
4847
4851
|
|
|
4848
4852
|
A grantee in the function's account gets an IAM policy statement. Another account or an AWS service also gets
|
|
4849
|
-
a statement in the function's resource policy
|
|
4853
|
+
a statement in the function's resource policy.
|
|
4850
4854
|
|
|
4851
4855
|
###### `grantee`<sup>Required</sup> <a name="grantee" id="@ashishpandeyone/cdk-lambda-web-function.WebFunctionGrants.invoke.parameter.grantee"></a>
|
|
4852
4856
|
|
|
@@ -4939,7 +4943,7 @@ WebFunctionOrigin.withOriginAccessControl(endpoint: WebFunctionEndpoint, props?:
|
|
|
4939
4943
|
|
|
4940
4944
|
An origin for an `AWS_IAM` endpoint, with origin access control: CloudFront signs every request to the endpoint (`always-amz-auth`, which keeps the viewer's `Authorization` header for your application), and the function's resource policy allows `cloudfront.amazonaws.com` to call this endpoint on behalf of this distribution only.
|
|
4941
4945
|
|
|
4942
|
-
The distribution
|
|
4946
|
+
The distribution can be in another stack than the function, in the same account.
|
|
4943
4947
|
|
|
4944
4948
|
###### `endpoint`<sup>Required</sup> <a name="endpoint" id="@ashishpandeyone/cdk-lambda-web-function.WebFunctionOrigin.withOriginAccessControl.parameter.endpoint"></a>
|
|
4945
4949
|
|
|
@@ -4965,9 +4969,9 @@ The distribution must be in the function's stack.
|
|
|
4965
4969
|
|
|
4966
4970
|
A Lambda Web Function, created in this app (`WebFunction`) or imported (`WebFunction.fromWebFunctionArn`).
|
|
4967
4971
|
|
|
4968
|
-
`addToResourcePolicy` adds to the function's resource policy (who else may call or manage it).
|
|
4969
|
-
|
|
4970
|
-
|
|
4972
|
+
`addToResourcePolicy` adds to the function's resource policy (who else may call or manage it). Each stack writes
|
|
4973
|
+
only its own statements into the function's single policy, so any stack in the function's account can add them,
|
|
4974
|
+
including one that imports the function. From another account, nothing is added (`statementAdded: false`).
|
|
4971
4975
|
|
|
4972
4976
|
#### Methods <a name="Methods" id="Methods"></a>
|
|
4973
4977
|
|
|
@@ -4993,9 +4997,8 @@ Allows `grantee` to call the function's endpoint (`lambda:InvokeWebFunctionEndpo
|
|
|
4993
4997
|
|
|
4994
4998
|
for an imported function, the endpoint named in its attributes, or every endpoint.
|
|
4995
4999
|
|
|
4996
|
-
|
|
4997
|
-
|
|
4998
|
-
changes: across accounts, the function's own resource policy must also allow the grantee.
|
|
5000
|
+
A grantee in the function's account gets an IAM policy statement; another account or an AWS service also gets a
|
|
5001
|
+
statement in the function's resource policy (not when the function itself is in another account than this stack).
|
|
4999
5002
|
[disable-awslint:no-grants]
|
|
5000
5003
|
|
|
5001
5004
|
###### `grantee`<sup>Required</sup> <a name="grantee" id="@ashishpandeyone/cdk-lambda-web-function.IWebFunction.grantInvoke.parameter.grantee"></a>
|
package/README.md
CHANGED
|
@@ -186,7 +186,9 @@ fn.addPermission('Cdn', {
|
|
|
186
186
|
|
|
187
187
|
`addPermission` allows calling every endpoint unless you pass `endpoint`. For other actions, such as letting another account create revisions, use `fn.addToResourcePolicy(statement)`. Statements use the endpoint ARN for invoking and the function ARN for actions on the function.
|
|
188
188
|
|
|
189
|
-
- **
|
|
189
|
+
- **Each stack writes only its own statements.** A Web Function has a single resource policy, and Web Functions have no `AddPermission`. So each stack that adds statements gets a custom resource (`Custom::LambdaWebResourcePolicyStatements`). It reads the policy, replaces the statements that stack wrote before, and writes the policy back, retrying if another writer changed it in the meantime. Statements from other stacks, and ones added outside CloudFormation, are kept. Writers include the function's stack, a stack with a CloudFront distribution, and a stack that imports the function in the same account.
|
|
190
|
+
- **Sids are unique in the policy.** `addPermission`'s `id` becomes the statement's `Sid`, and other statements get a generated one (`Cdk<hash><n>`). If another stack or tool already uses that `Sid` for different content, the deployment fails with the `Sid`.
|
|
191
|
+
- **Don't also attach an `AWS::Lambda::ResourcePolicy` yourself.** That resource replaces the whole document on every update, dropping every other writer's statements.
|
|
190
192
|
- **Public policies are rejected.** Lambda refuses a policy that lets anyone call the function, and the construct fails synthesis first. A `*` principal is only accepted with a condition that pins the caller, such as `aws:PrincipalOrgID` (`iam.OrganizationPrincipal`) or `aws:SourceAccount`. For a public endpoint, use `authType: NONE`.
|
|
191
193
|
- **Scope AWS services to your resource.** Lambda accepts a service principal with no condition, which would let that service call the endpoint on behalf of any account. The construct warns unless you give `sourceArn` or `sourceAccount` (or an `aws:SourceArn`, `aws:SourceAccount`, `aws:SourceOrgID` or `aws:SourceOrgPaths` condition).
|
|
192
194
|
- Lambda copies the policy to every region the function's endpoints serve from. The copy is eventually consistent, and in tests changes applied in every region within seconds.
|
|
@@ -226,7 +228,10 @@ Some things to know:
|
|
|
226
228
|
- Request bodies work without clients sending `x-amz-content-sha256`: CloudFront signs `POST`, `PUT` and `PATCH` requests with an unsigned payload.
|
|
227
229
|
- A `PER_REGION` endpoint's origin calls one region: the home region, or the one you pass as `region`. Use one origin per region, for example in an origin group.
|
|
228
230
|
- A `MULTI_REGION` endpoint can't include opt-in regions (such as `me-central-1`), because CloudFront can't sign for them. The construct fails synthesis.
|
|
229
|
-
- The distribution
|
|
231
|
+
- The distribution can be in another stack, in the function's account. Its stack writes the CloudFront statement into the function's resource policy, so the function's stack doesn't depend on it.
|
|
232
|
+
- That stack can also be in another region, for example us-east-1 for an ACM certificate. CDK then passes the endpoint's domain and ARN with its cross-region references: SSM parameters plus custom resources of its own.
|
|
233
|
+
- **Those references are strong by default.** A change that replaces the endpoint gives it a new domain and ARN. That includes a new endpoint type or region set, or a renamed function. The function's stack can't update a value while the distribution's stack still uses the old one, the same as with `Fn::ImportValue` in one region. Deploy such a change in two steps, or use weak references: CDK's synth warning `@aws-cdk/core:crossStackReferencesDefaultStrong` explains how.
|
|
234
|
+
- Lambda@Edge origin-request and origin-response triggers aren't supported on behaviors with this origin.
|
|
230
235
|
- The endpoint stays reachable at its own domain for signed requests your policies allow. Until a new distribution finishes deploying, requests through it can get a transient 403.
|
|
231
236
|
- `new WebFunctionOrigin(endpoint)` is for a public (`NONE`) endpoint, for example behind AWS WAF. Lambda doesn't authenticate those requests, so the endpoint's own domain stays public too.
|
|
232
237
|
|
|
@@ -247,7 +252,7 @@ live.metricConcurrentRequests().createAlarm(this, 'Busy', { threshold: 60, evalu
|
|
|
247
252
|
|
|
248
253
|
An imported function's metrics default to its own region. Its endpoints, revisions and log groups stay with the stack that defines it.
|
|
249
254
|
|
|
250
|
-
An imported function
|
|
255
|
+
An imported function works like a `WebFunction` here. `grantInvoke` and `addToResourcePolicy` write the importing stack's statements into the function's resource policy, in the function's region. This works only when the function is in the stack's account. For a function in another account, `grantInvoke` adds to the caller's IAM policy only, and `addToResourcePolicy` adds nothing and warns, so ask that account to allow the caller.
|
|
251
256
|
|
|
252
257
|
## Configuration
|
|
253
258
|
|
|
@@ -271,6 +276,7 @@ table.grantReadWriteData(api); // the default role trusts lambda.amazo
|
|
|
271
276
|
- **Each execution environment has 2 vCPUs and 2 GB of memory.** There's no memory setting as on Lambda functions; scale with `maxConcurrencyPerEnvironment` and more environments instead.
|
|
272
277
|
- Requests handled by one environment share module-level state, so keep handlers concurrency-safe.
|
|
273
278
|
- Environment variable names starting with `AWS_` are reserved.
|
|
279
|
+
- **The default execution role can only write to the function's own log groups**: the home region's, and the `.local` copy in every region it serves from. That's the minimum AWS documents for Web Functions. It doesn't use `AWSLambdaBasicExecutionRole`, which can also create any log group. Pass `role` to use your own, and include those log permissions.
|
|
274
280
|
- **Renaming the function replaces it and changes its URL.** A generated name depends on the construct path, so moving the construct renames it too.
|
|
275
281
|
|
|
276
282
|
### Bundling TypeScript with `NodejsWebFunction`
|
|
@@ -458,6 +464,35 @@ new WebFunction(this, 'Api', {
|
|
|
458
464
|
- With `DESTROY`, deletion waits about a minute after the function is gone, because environments flush their last lines late.
|
|
459
465
|
- Subscribe only to `fn.logGroup`: subscribing to the `.local` groups as well delivers every event twice.
|
|
460
466
|
|
|
467
|
+
## cdk-nag
|
|
468
|
+
|
|
469
|
+
The library acknowledges the [cdk-nag](https://github.com/cdklabs/cdk-nag) AwsSolutions findings on the resources it creates, each with a reason, so they don't show up in your reports:
|
|
470
|
+
|
|
471
|
+
| Finding | Resource | Reason |
|
|
472
|
+
|---|---|---|
|
|
473
|
+
| IAM5 (wildcards) | the default execution role | log streams of the function's own log groups, in every region it serves from |
|
|
474
|
+
| IAM5 | the deployment handlers' role (canary and linear deployments) | their own log group, `cloudwatch:DescribeAlarms` (composite alarms need `*`), and the revisions and endpoints of the functions they deploy |
|
|
475
|
+
| IAM4, IAM5, SF1, SF2 | the CDK Provider framework behind deployments | CDK's own functions and waiter state machine |
|
|
476
|
+
| S1 (access logs) | the stack's code bucket | it only holds copies of the stack's code assets |
|
|
477
|
+
|
|
478
|
+
- **This works with both cdk-nag versions**, without depending on cdk-nag. cdk-nag 3 reads CDK's `Validations.of(...).acknowledge()`, one ID per finding. cdk-nag 2 reads `cdk_nag` metadata in the template.
|
|
479
|
+
- **Your own statements and resources are still checked.** That includes statements you add to the function's role (`addToRolePolicy`, grants) and your CloudFront distribution's settings.
|
|
480
|
+
- Only the AwsSolutions pack is covered; findings from the HIPAA, NIST and PCI packs aren't acknowledged.
|
|
481
|
+
|
|
482
|
+
## Upgrading
|
|
483
|
+
|
|
484
|
+
### From 0.1.5 to 0.1.7, with a resource policy
|
|
485
|
+
|
|
486
|
+
Versions 0.1.5 to 0.1.7 wrote the function's resource policy with an `AWS::Lambda::ResourcePolicy`. Later versions don't (0.1.7 has the same code as 0.1.6). When an update removes that resource, CloudFormation deletes the function's **whole** resource policy at the end of the update, including the statements the new version just wrote. Callers that rely on the policy then get 403: other accounts, AWS services, and CloudFront.
|
|
487
|
+
|
|
488
|
+
If your function has a resource policy (from `addPermission`, a grant to another account or an AWS service, or `WebFunctionOrigin`), first deploy once on the version you have, with the old resource retained:
|
|
489
|
+
|
|
490
|
+
```ts
|
|
491
|
+
(fn.node.findChild('ResourcePolicy') as CfnResource).applyRemovalPolicy(RemovalPolicy.RETAIN);
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
Then upgrade, without changing your statements in the same deployment. The new version takes over the statements it writes itself. A statement you removed in the same deployment would stay in the policy.
|
|
495
|
+
|
|
461
496
|
## Limitations
|
|
462
497
|
|
|
463
498
|
- Not yet supported: VPC access (network connectors), KMS encryption, and tracing.
|