@byaga/cdk-patterns 0.10.0 → 0.11.1

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.
Files changed (99) hide show
  1. package/README.md +7 -1
  2. package/archive/ApiAttachPoint.ts +9 -0
  3. package/archive/ApiCertificate.ts +32 -0
  4. package/{lib/CognitoApiGatewayAuthorizer.d.ts → archive/CognitoApiGatewayAuthorizer.ts} +13 -7
  5. package/archive/DynamoDbTable.ts +40 -0
  6. package/archive/ICorsConfig.ts +5 -0
  7. package/archive/IDomainConfig.ts +8 -0
  8. package/archive/IHostedZoneConfig.ts +5 -0
  9. package/archive/NodeJsLambdaLayer.ts +42 -0
  10. package/archive/Output.ts +11 -0
  11. package/{lib/RestApi.js → archive/RestApi.ts} +96 -60
  12. package/{lib/Role.d.ts → archive/Role.ts} +165 -139
  13. package/archive/StaticWebSite.ts +159 -0
  14. package/archive/index.ts +18 -0
  15. package/archive/methods/apply-schema-to-method-options.ts +38 -0
  16. package/archive/methods/attach-function-to-api.ts +69 -0
  17. package/lib/{methods → build}/copy-files.js +1 -1
  18. package/lib/{methods → build/nodejs}/build-ecmascript.d.ts +2 -2
  19. package/lib/{methods → build/nodejs}/build-ecmascript.js +5 -5
  20. package/lib/{methods → build/nodejs}/build-node-source.d.ts +3 -1
  21. package/lib/{methods → build/nodejs}/build-node-source.js +6 -6
  22. package/lib/{methods → build/nodejs}/build-typescript.d.ts +2 -2
  23. package/lib/{methods → build/nodejs}/build-typescript.js +5 -5
  24. package/lib/{methods → build/nodejs}/install-node-modules.js +1 -1
  25. package/lib/create-stack.d.ts +41 -0
  26. package/lib/create-stack.js +48 -0
  27. package/lib/generate-identifier.d.ts +27 -0
  28. package/lib/generate-identifier.js +65 -0
  29. package/lib/index.d.ts +8 -15
  30. package/lib/index.js +40 -25
  31. package/lib/lambda/create-function.d.ts +20 -0
  32. package/lib/lambda/create-function.js +39 -0
  33. package/lib/lambda/create-nodejs-lambda.d.ts +28 -0
  34. package/lib/lambda/create-nodejs-lambda.js +38 -0
  35. package/lib/lambda-layer/apply-honeycomb-to-lambda.d.ts +2 -0
  36. package/lib/lambda-layer/apply-honeycomb-to-lambda.js +25 -0
  37. package/lib/lambda-layer/get-layer.d.ts +2 -0
  38. package/lib/lambda-layer/get-layer.js +20 -0
  39. package/lib/lambda-layer/layer-cache.d.ts +3 -0
  40. package/lib/lambda-layer/layer-cache.js +12 -0
  41. package/lib/load-configuration.d.ts +12 -0
  42. package/lib/load-configuration.js +42 -0
  43. package/lib/ssm/SsmParameter.d.ts +12 -0
  44. package/lib/ssm/get-existing-parameter.d.ts +9 -0
  45. package/lib/ssm/get-existing-parameter.js +41 -0
  46. package/lib/ssm/grant-read.d.ts +7 -0
  47. package/lib/ssm/grant-read.js +24 -0
  48. package/lib/ssm/index.d.ts +3 -0
  49. package/lib/ssm/index.js +9 -0
  50. package/lib/ssm/parameter-cache.d.ts +13 -0
  51. package/lib/ssm/parameter-cache.js +23 -0
  52. package/lib/ssm/string-value.d.ts +8 -0
  53. package/lib/ssm/string-value.js +14 -0
  54. package/package.json +4 -3
  55. package/lib/ApiCertificate.d.ts +0 -10
  56. package/lib/ApiCertificate.js +0 -31
  57. package/lib/CognitoApiGatewayAuthorizer.js +0 -24
  58. package/lib/DeployStack.d.ts +0 -19
  59. package/lib/DeployStack.js +0 -57
  60. package/lib/DynamoDbTable.d.ts +0 -12
  61. package/lib/DynamoDbTable.js +0 -34
  62. package/lib/FunctionIntegration.d.ts +0 -25
  63. package/lib/FunctionIntegration.js +0 -65
  64. package/lib/ICorsConfig.d.ts +0 -5
  65. package/lib/IDomainConfig.d.ts +0 -7
  66. package/lib/IDomainConfig.js +0 -2
  67. package/lib/IHostedZoneConfig.d.ts +0 -5
  68. package/lib/IHostedZoneConfig.js +0 -2
  69. package/lib/IStackArguments.d.ts +0 -8
  70. package/lib/IStackArguments.js +0 -2
  71. package/lib/NodeJsLambda.d.ts +0 -35
  72. package/lib/NodeJsLambda.js +0 -44
  73. package/lib/NodeJsLambdaLayer.d.ts +0 -25
  74. package/lib/NodeJsLambdaLayer.js +0 -35
  75. package/lib/Output.d.ts +0 -5
  76. package/lib/Output.js +0 -13
  77. package/lib/RestApi.d.ts +0 -28
  78. package/lib/Role.js +0 -27
  79. package/lib/SsmParameter.d.ts +0 -18
  80. package/lib/SsmParameter.js +0 -40
  81. package/lib/StaticWebSite.d.ts +0 -33
  82. package/lib/StaticWebSite.js +0 -136
  83. package/lib/methods/BuildOptions.d.ts +0 -3
  84. package/lib/methods/BuildOptions.js +0 -2
  85. package/lib/methods/apply-honeycomb-to-lambda.d.ts +0 -3
  86. package/lib/methods/apply-honeycomb-to-lambda.js +0 -23
  87. /package/lib/{methods → build}/copy-files.d.ts +0 -0
  88. /package/lib/{methods → build}/generate-hash.d.ts +0 -0
  89. /package/lib/{methods → build}/generate-hash.js +0 -0
  90. /package/lib/{methods → build}/get-files.d.ts +0 -0
  91. /package/lib/{methods → build}/get-files.js +0 -0
  92. /package/lib/{methods → build}/get-source-directory.d.ts +0 -0
  93. /package/lib/{methods → build}/get-source-directory.js +0 -0
  94. /package/lib/{methods → build}/hash-file.d.ts +0 -0
  95. /package/lib/{methods → build}/hash-file.js +0 -0
  96. /package/lib/{methods → build/nodejs}/install-node-modules.d.ts +0 -0
  97. /package/lib/{ICorsConfig.js → ssm/SsmParameter.js} +0 -0
  98. /package/lib/{methods → tools}/duration.d.ts +0 -0
  99. /package/lib/{methods → tools}/duration.js +0 -0
@@ -1,139 +1,165 @@
1
- import { IPrincipal, Role as CdkRole } from "aws-cdk-lib/aws-iam";
2
- import { Duration } from "aws-cdk-lib";
3
- import { DeployStack } from "./DeployStack";
4
- import { IManagedPolicy } from "aws-cdk-lib/aws-iam/lib/managed-policy";
5
- import { PolicyDocument } from "aws-cdk-lib/aws-iam/lib/policy-document";
6
- interface IRoleProps {
7
- identityPool: string;
8
- amr: string;
9
- /**
10
- * The IAM principal (i.e. `new ServicePrincipal('sns.amazonaws.com')`) which can assume this role.
11
- *
12
- * You can later modify the assume role policy document by accessing it via
13
- * the `assumeRolePolicy` property.
14
- *
15
- * @stability stable
16
- */
17
- assumedBy?: IPrincipal;
18
- /**
19
- * (deprecated) ID that the role assumer needs to provide when assuming this role.
20
- *
21
- * If the configured and provided external IDs do not match, the
22
- * AssumeRole operation will fail.
23
- *
24
- * @default No external ID required
25
- * @deprecated see {@link externalIds}
26
- */
27
- readonly externalId?: string;
28
- /**
29
- * List of IDs that the role assumer needs to provide one of when assuming this role.
30
- *
31
- * If the configured and provided external IDs do not match, the
32
- * AssumeRole operation will fail.
33
- *
34
- * @default No external ID required
35
- * @stability stable
36
- */
37
- readonly externalIds?: string[];
38
- /**
39
- * A list of managed policies associated with this role.
40
- *
41
- * You can add managed policies later using
42
- * `addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName(policyName))`.
43
- *
44
- * @default - No managed policies.
45
- * @stability stable
46
- */
47
- readonly managedPolicies?: IManagedPolicy[];
48
- /**
49
- * A list of named policies to inline into this role.
50
- *
51
- * These policies will be
52
- * created with the role, whereas those added by ``addToPolicy`` are added
53
- * using a separate CloudFormation resource (allowing a way around circular
54
- * dependencies that could otherwise be introduced).
55
- *
56
- * @default - No policy is inlined in the Role resource.
57
- * @stability stable
58
- */
59
- readonly inlinePolicies?: {
60
- [name: string]: PolicyDocument;
61
- };
62
- /**
63
- * The path associated with this role.
64
- *
65
- * For information about IAM paths, see
66
- * Friendly Names and Paths in IAM User Guide.
67
- *
68
- * @default /
69
- * @stability stable
70
- */
71
- readonly path?: string;
72
- /**
73
- * AWS supports permissions boundaries for IAM entities (users or roles).
74
- *
75
- * A permissions boundary is an advanced feature for using a managed policy
76
- * to set the maximum permissions that an identity-based policy can grant to
77
- * an IAM entity. An entity's permissions boundary allows it to perform only
78
- * the actions that are allowed by both its identity-based policies and its
79
- * permissions boundaries.
80
- *
81
- * @default - No permissions boundary.
82
- * @stability stable
83
- * @link https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_boundaries.html
84
- */
85
- readonly permissionsBoundary?: IManagedPolicy;
86
- /**
87
- * A name for the IAM role.
88
- *
89
- * For valid values, see the RoleName parameter for
90
- * the CreateRole action in the IAM API Reference.
91
- *
92
- * IMPORTANT: If you specify a name, you cannot perform updates that require
93
- * replacement of this resource. You can perform updates that require no or
94
- * some interruption. If you must replace the resource, specify a new name.
95
- *
96
- * If you specify a name, you must specify the CAPABILITY_NAMED_IAM value to
97
- * acknowledge your template's capabilities. For more information, see
98
- * Acknowledging IAM Resources in AWS CloudFormation Templates.
99
- *
100
- * @default - AWS CloudFormation generates a unique physical ID and uses that ID
101
- * for the role name.
102
- * @stability stable
103
- */
104
- readonly roleName?: string;
105
- /**
106
- * The maximum session duration that you want to set for the specified role.
107
- *
108
- * This setting can have a value from 1 hour (3600sec) to 12 (43200sec) hours.
109
- *
110
- * Anyone who assumes the role from the AWS CLI or API can use the
111
- * DurationSeconds API parameter or the duration-seconds CLI parameter to
112
- * request a longer session. The MaxSessionDuration setting determines the
113
- * maximum duration that can be requested using the DurationSeconds
114
- * parameter.
115
- *
116
- * If users don't specify a value for the DurationSeconds parameter, their
117
- * security credentials are valid for one hour by default. This applies when
118
- * you use the AssumeRole* API operations or the assume-role* CLI operations
119
- * but does not apply when you use those operations to create a console URL.
120
- *
121
- * @default Duration.hours(1)
122
- * @stability stable
123
- * @link https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use.html
124
- */
125
- readonly maxSessionDuration?: Duration;
126
- /**
127
- * A description of the role.
128
- *
129
- * It can be up to 1000 characters long.
130
- *
131
- * @default - No description.
132
- * @stability stable
133
- */
134
- readonly description?: string;
135
- }
136
- export declare class Role extends CdkRole {
137
- constructor(stack: DeployStack, id: string, props: IRoleProps);
138
- }
139
- export {};
1
+ import {FederatedPrincipal, IPrincipal, Role as CdkRole} from "aws-cdk-lib/aws-iam";
2
+ import {Duration} from "aws-cdk-lib";
3
+ import {Output} from './Output'
4
+ import {DeployStack} from "./DeployStack";
5
+ import {IManagedPolicy} from "aws-cdk-lib/aws-iam/lib/managed-policy";
6
+ import {PolicyDocument} from "aws-cdk-lib/aws-iam/lib/policy-document";
7
+
8
+ interface IRoleProps {
9
+ identityPool: string,
10
+ amr: string,
11
+ /**
12
+ * The IAM principal (i.e. `new ServicePrincipal('sns.amazonaws.com')`) which can assume this role.
13
+ *
14
+ * You can later modify the assume role policy document by accessing it via
15
+ * the `assumeRolePolicy` property.
16
+ *
17
+ * @stability stable
18
+ */
19
+ assumedBy?: IPrincipal,
20
+
21
+ /**
22
+ * (deprecated) ID that the role assumer needs to provide when assuming this role.
23
+ *
24
+ * If the configured and provided external IDs do not match, the
25
+ * AssumeRole operation will fail.
26
+ *
27
+ * @default No external ID required
28
+ * @deprecated see {@link externalIds}
29
+ */
30
+ readonly externalId?: string;
31
+ /**
32
+ * List of IDs that the role assumer needs to provide one of when assuming this role.
33
+ *
34
+ * If the configured and provided external IDs do not match, the
35
+ * AssumeRole operation will fail.
36
+ *
37
+ * @default No external ID required
38
+ * @stability stable
39
+ */
40
+ readonly externalIds?: string[];
41
+ /**
42
+ * A list of managed policies associated with this role.
43
+ *
44
+ * You can add managed policies later using
45
+ * `addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName(policyName))`.
46
+ *
47
+ * @default - No managed policies.
48
+ * @stability stable
49
+ */
50
+ readonly managedPolicies?: IManagedPolicy[];
51
+ /**
52
+ * A list of named policies to inline into this role.
53
+ *
54
+ * These policies will be
55
+ * created with the role, whereas those added by ``addToPolicy`` are added
56
+ * using a separate CloudFormation resource (allowing a way around circular
57
+ * dependencies that could otherwise be introduced).
58
+ *
59
+ * @default - No policy is inlined in the Role resource.
60
+ * @stability stable
61
+ */
62
+ readonly inlinePolicies?: {
63
+ [name: string]: PolicyDocument;
64
+ };
65
+ /**
66
+ * The path associated with this role.
67
+ *
68
+ * For information about IAM paths, see
69
+ * Friendly Names and Paths in IAM User Guide.
70
+ *
71
+ * @default /
72
+ * @stability stable
73
+ */
74
+ readonly path?: string;
75
+ /**
76
+ * AWS supports permissions boundaries for IAM entities (users or roles).
77
+ *
78
+ * A permissions boundary is an advanced feature for using a managed policy
79
+ * to set the maximum permissions that an identity-based policy can grant to
80
+ * an IAM entity. An entity's permissions boundary allows it to perform only
81
+ * the actions that are allowed by both its identity-based policies and its
82
+ * permissions boundaries.
83
+ *
84
+ * @default - No permissions boundary.
85
+ * @stability stable
86
+ * @link https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_boundaries.html
87
+ */
88
+ readonly permissionsBoundary?: IManagedPolicy;
89
+ /**
90
+ * A name for the IAM role.
91
+ *
92
+ * For valid values, see the RoleName parameter for
93
+ * the CreateRole action in the IAM API Reference.
94
+ *
95
+ * IMPORTANT: If you specify a name, you cannot perform updates that require
96
+ * replacement of this resource. You can perform updates that require no or
97
+ * some interruption. If you must replace the resource, specify a new name.
98
+ *
99
+ * If you specify a name, you must specify the CAPABILITY_NAMED_IAM value to
100
+ * acknowledge your template's capabilities. For more information, see
101
+ * Acknowledging IAM Resources in AWS CloudFormation Templates.
102
+ *
103
+ * @default - AWS CloudFormation generates a unique physical ID and uses that ID
104
+ * for the role name.
105
+ * @stability stable
106
+ */
107
+ readonly roleName?: string;
108
+ /**
109
+ * The maximum session duration that you want to set for the specified role.
110
+ *
111
+ * This setting can have a value from 1 hour (3600sec) to 12 (43200sec) hours.
112
+ *
113
+ * Anyone who assumes the role from the AWS CLI or API can use the
114
+ * DurationSeconds API parameter or the duration-seconds CLI parameter to
115
+ * request a longer session. The MaxSessionDuration setting determines the
116
+ * maximum duration that can be requested using the DurationSeconds
117
+ * parameter.
118
+ *
119
+ * If users don't specify a value for the DurationSeconds parameter, their
120
+ * security credentials are valid for one hour by default. This applies when
121
+ * you use the AssumeRole* API operations or the assume-role* CLI operations
122
+ * but does not apply when you use those operations to create a console URL.
123
+ *
124
+ * @default Duration.hours(1)
125
+ * @stability stable
126
+ * @link https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use.html
127
+ */
128
+ readonly maxSessionDuration?: Duration;
129
+ /**
130
+ * A description of the role.
131
+ *
132
+ * It can be up to 1000 characters long.
133
+ *
134
+ * @default - No description.
135
+ * @stability stable
136
+ */
137
+ readonly description?: string;
138
+ }
139
+
140
+ export class Role extends CdkRole {
141
+ constructor(stack: DeployStack, id: string, props: IRoleProps) {
142
+ console.log('Defining Role', stack.genId(id))
143
+ if (!props.assumedBy) {
144
+ props.assumedBy = new FederatedPrincipal(
145
+ 'cognito-identity.amazonaws.com',
146
+ {
147
+ StringEquals: {
148
+ 'cognito-identity.amazonaws.com:aud': props.identityPool
149
+ },
150
+ "ForAnyValue:StringLike": {
151
+ 'cognito-identity.amazonaws.com:amr': props.amr
152
+ }
153
+ },
154
+ 'sts:AssumeRoleWithWebIdentity'
155
+ )
156
+ }
157
+
158
+ super(stack, stack.genId(id), {
159
+ ...props,
160
+ roleName: stack.genName(props.roleName || id),
161
+ assumedBy: props.assumedBy
162
+ })
163
+ new Output(stack, `${id}-arn`, this.roleArn)
164
+ }
165
+ }
@@ -0,0 +1,159 @@
1
+ import {RemovalPolicy} from "aws-cdk-lib/core";
2
+ import {Output} from './Output'
3
+ import {DeployStack} from "./DeployStack";
4
+ import {execSync} from "child_process";
5
+ import {Bucket} from "aws-cdk-lib/aws-s3"
6
+ import {BucketDeployment, Source} from "aws-cdk-lib/aws-s3-deployment"
7
+ import ApiCertificate from "./ApiCertificate";
8
+ import {
9
+ CloudFrontAllowedMethods,
10
+ CloudFrontWebDistribution,
11
+ OriginAccessIdentity,
12
+ OriginProtocolPolicy, SourceConfiguration,
13
+ SSLMethod,
14
+ ViewerCertificate,
15
+ ViewerProtocolPolicy
16
+ } from "aws-cdk-lib/aws-cloudfront"
17
+ import IDomainConfig from "./IDomainConfig";
18
+ import {PolicyStatement} from "aws-cdk-lib/aws-iam";
19
+ import {ARecord, RecordTarget} from "aws-cdk-lib/aws-route53";
20
+ import {CloudFrontTarget} from "aws-cdk-lib/aws-route53-targets";
21
+ import duration from './methods/duration';
22
+ import {buildNodeSource} from './methods/build-node-source'
23
+ import {Duration} from "aws-cdk-lib";
24
+
25
+ /**
26
+ * Configuration interface for the StaticWebSite class.
27
+ */
28
+ export interface StaticWebSiteConfig {
29
+ srcDir?: string,
30
+ domain: IDomainConfig,
31
+ env: NodeJS.ProcessEnv,
32
+ proxy?: SourceConfiguration[]
33
+ }
34
+
35
+ /**
36
+ * Logic which will setup a static web site in AWS with an S3 Bucket, CloudFront Distribution, and Route53 A-Name Record.
37
+ */
38
+ export class StaticWebSite {
39
+ /**
40
+ * Constructs a new StaticWebSite instance.
41
+ * @param {DeployStack} stack - The deployment stack.
42
+ * @param {string} id - The ID of the website.
43
+ * @param {StaticWebSiteConfig} props - The configuration properties of the website.
44
+ */
45
+ constructor(stack: DeployStack, id: string, props: StaticWebSiteConfig) {
46
+ console.log('Deploying Static Web Site', id)
47
+
48
+ const done = duration()
49
+ const buildDir = buildNodeSource('web', id)
50
+
51
+ console.log('Building UI Source', id)
52
+ execSync('npm run export', {
53
+ cwd: buildDir,
54
+ env: props.env
55
+ })
56
+
57
+ console.log('Total Build Duration (ms)', done())
58
+ const exportDir: string = buildDir + '/out';
59
+
60
+ console.log('Creating HTTPS Certificate', id + '-certificate')
61
+ const certificate = new ApiCertificate(stack, id + '-certificate', props.domain);
62
+ console.log('Deploying Site Content Bucket', stack.genId(id, "content-bucket"))
63
+ const s3BucketSource = new Bucket(stack, stack.genId(id, "content-bucket"), {
64
+ bucketName: certificate.domain,
65
+ websiteIndexDocument: "index.html",
66
+ websiteErrorDocument: "error.html",
67
+ removalPolicy: RemovalPolicy.DESTROY,
68
+ autoDeleteObjects: true,
69
+ publicReadAccess: false,
70
+ blockPublicAccess: {
71
+ blockPublicAcls: true,
72
+ blockPublicPolicy: true,
73
+ ignorePublicAcls: true,
74
+ restrictPublicBuckets: true
75
+ }
76
+ });
77
+ new Output(stack, `${id}-content-bucket`, s3BucketSource.bucketName);
78
+
79
+ const cloudFrontPolicy = new PolicyStatement({
80
+ actions: ['s3:GetBucket*', 's3:GetObject*', 's3:List*'],
81
+ resources: [s3BucketSource.bucketArn, s3BucketSource.bucketArn + '/*'],
82
+ })
83
+
84
+ new BucketDeployment(stack, stack.genId(id, 'bucket-deployment'), {
85
+ sources: [Source.asset(exportDir)],
86
+ destinationBucket: s3BucketSource
87
+ })
88
+ const originAccessIdentity = new OriginAccessIdentity(stack, "WebsiteOAI");
89
+ cloudFrontPolicy.addArnPrincipal(originAccessIdentity.cloudFrontOriginAccessIdentityS3CanonicalUserId);
90
+ s3BucketSource.grantRead(originAccessIdentity)
91
+
92
+ const distro = new CloudFrontWebDistribution(stack, stack.genId(id, 'cloudfront-web-distribution'), {
93
+ enabled: true,
94
+ originConfigs: [...props.proxy || [], {
95
+ s3OriginSource: {
96
+ s3BucketSource,
97
+ originAccessIdentity
98
+ },
99
+ behaviors: [{
100
+ allowedMethods: CloudFrontAllowedMethods.GET_HEAD_OPTIONS,
101
+ isDefaultBehavior: true
102
+ }]
103
+ }],
104
+ viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
105
+ viewerCertificate: ViewerCertificate.fromAcmCertificate(certificate, {
106
+ aliases: [certificate.domain],
107
+ sslMethod: SSLMethod.SNI
108
+ })
109
+ })
110
+ new Output(stack, `${id}-base-url`, "https://" + certificate.domain);
111
+
112
+ console.log('Setting Route53 A-Name Record', props.domain.domainName)
113
+ new ARecord(stack, stack.genId('alias'), {
114
+ zone: certificate.hostedZone,
115
+ recordName: certificate.domain,
116
+ target: RecordTarget.fromAlias(new CloudFrontTarget(distro))
117
+ })
118
+ }
119
+
120
+ /**
121
+ * Defines a proxy for the static website.
122
+ * @param {DeployStack} stack - The deployment stack.
123
+ * @param {string} domainName - The domain name.
124
+ * @param {string} pathPattern - The path pattern.
125
+ * @returns {SourceConfiguration} The source configuration for the proxy.
126
+ */
127
+ static defineProxy(stack: DeployStack, domainName: string, pathPattern: string): SourceConfiguration {
128
+ return {
129
+ customOriginSource: {
130
+ domainName: domainName,
131
+ originPath: `/${stack.stage}`,
132
+ originProtocolPolicy: OriginProtocolPolicy.HTTPS_ONLY
133
+ },
134
+ behaviors: [{
135
+ pathPattern,
136
+ allowedMethods: CloudFrontAllowedMethods.ALL,
137
+ maxTtl: Duration.seconds(0),
138
+ minTtl: Duration.seconds(0),
139
+ defaultTtl: Duration.seconds(0),
140
+ compress: false,
141
+ forwardedValues: {
142
+ queryString: true,
143
+ headers: [
144
+ "Authorization",
145
+ "User-Agent",
146
+ "X-Trace-Id",
147
+ "X-Span-Id",
148
+ "X-Correlation-Id",
149
+ 'Referer',
150
+ "User-Agent",
151
+ "Accept-Language",
152
+ 'Content-Type',
153
+ 'Content-Length'
154
+ ]
155
+ }
156
+ }]
157
+ }
158
+ }
159
+ }
@@ -0,0 +1,18 @@
1
+ export {ApiCertificate} from "./ApiCertificate";
2
+ export {CognitoApiGatewayAuthorizer} from "./CognitoApiGatewayAuthorizer";
3
+ export {DeployStack} from "./DeployStack";
4
+ export {DynamoDbTable} from "./DynamoDbTable";
5
+ export {IHostedZoneConfig} from "./IHostedZoneConfig";
6
+ export {IDomainConfig} from "./IDomainConfig";
7
+ export {IStackArguments} from "./IStackArguments";
8
+ export {NodeJsLambdaLayer} from "./NodeJsLambdaLayer";
9
+ export {Output} from "./Output";
10
+ export {RestApi} from "./RestApi";
11
+ export {Role} from "./Role";
12
+ export {SsmParameter} from "./SsmParameter";
13
+ export {StaticWebSite} from "./StaticWebSite";
14
+
15
+ export {createFunction} from "./create-function";
16
+ export {createNodejsLambda} from "./create-nodejs-lambda";
17
+ export {attachFunctionToApi} from "./methods/attach-function-to-api";
18
+ export {applySchemaToMethodOptions} from "./methods/apply-schema-to-method-options";
@@ -0,0 +1,38 @@
1
+ import {DeployStack} from "../DeployStack";
2
+ import {
3
+ JsonSchema,
4
+ MethodOptions,
5
+ Model,
6
+ RequestValidator,
7
+ IResource
8
+ } from "aws-cdk-lib/aws-apigateway";
9
+
10
+ /**
11
+ * Applies a schema to a method.
12
+ * @param {DeployStack} stack - The deployment stack.
13
+ * @param {string} name - The name of the method.
14
+ * @param {JsonSchema} schema - The schema.
15
+ * @param {MethodOptions} options - The method options.
16
+ * @param {IResource} path - The path.
17
+ * @returns {MethodOptions} The method options with the schema applied.
18
+ */
19
+ export function applySchemaToMethodOptions(stack: DeployStack, name: string, schema: JsonSchema, options: MethodOptions, path: IResource): MethodOptions {
20
+ return {
21
+ ...options,
22
+ requestValidator: new RequestValidator(stack, stack.genId(name, 'validator'), {
23
+ restApi: path.api,
24
+ requestValidatorName: stack.genName(name, 'validator'),
25
+ validateRequestBody: true
26
+ }),
27
+ requestModels: {
28
+ 'application/json': new Model(stack, stack.genId(name, 'model'), {
29
+ schema: schema,
30
+ contentType: 'application/json',
31
+ restApi: path.api,
32
+ modelName: stack.genId(name, 'model')
33
+ })
34
+ }
35
+ }
36
+ }
37
+
38
+ export default applySchemaToMethodOptions;
@@ -0,0 +1,69 @@
1
+ import {DeployStack} from "../DeployStack";
2
+ import {
3
+ JsonSchema,
4
+ LambdaIntegration,
5
+ MethodOptions,
6
+ IResource, Method
7
+ } from "aws-cdk-lib/aws-apigateway";
8
+ import {RestApi} from "../RestApi";
9
+ import {applySchemaToMethodOptions} from "./apply-schema-to-method-options";
10
+ import {FunctionIntegration} from "../create-function";
11
+
12
+ /**
13
+ * Type alias for the properties when attaching a function to an API or a resource.
14
+ */
15
+ type AttachFunctionToApiProps = AttachFunctionToRestApiProps | AttachFunctionToResourceProps
16
+
17
+ /**
18
+ * Interface for the properties when attaching a function to an API.
19
+ */
20
+ export interface AttachFunctionToRestApiProps {
21
+ api: RestApi
22
+ httpMethod: string
23
+ path: string
24
+ requestSchema?: JsonSchema,
25
+ methodOptions?: MethodOptions
26
+ }
27
+
28
+ /**
29
+ * Interface for the properties when attaching a function to a resource.
30
+ */
31
+ export interface AttachFunctionToResourceProps {
32
+ resource: IResource
33
+ httpMethod: string
34
+ requestSchema?: JsonSchema,
35
+ methodOptions?: MethodOptions
36
+ }
37
+
38
+ export interface ApiAttachedFunction extends FunctionIntegration {
39
+ resourceArn: string
40
+ method: Method
41
+ }
42
+
43
+ /**
44
+ * Attaches a function to an API or a resource.
45
+ * @param {DeployStack} stack - The deployment stack.
46
+ * @param lambda - The Function that is being attached
47
+ * @param {AttachFunctionToApiProps} props - The properties for attaching the function.
48
+ * @returns {ApiAttachPoint} The API attach point.
49
+ */
50
+ export function attachFunctionToApi(stack: DeployStack, lambda: FunctionIntegration, props: AttachFunctionToApiProps) : ApiAttachedFunction {
51
+ const resource: IResource = (props as AttachFunctionToResourceProps).resource || (props as AttachFunctionToRestApiProps).api.path((props as AttachFunctionToRestApiProps).path)
52
+ const integration = new LambdaIntegration(lambda.function, {
53
+ requestTemplates: {
54
+ 'application/json': '{ "statusCode": "200" }'
55
+ }
56
+ })
57
+
58
+ let options: MethodOptions = props?.methodOptions || {}
59
+ const schema = props?.requestSchema
60
+ if (schema) options = applySchemaToMethodOptions(stack, lambda.id, schema, options, resource)
61
+
62
+ const method = resource.addMethod(props.httpMethod, integration, options)
63
+
64
+ return {
65
+ ...lambda,
66
+ method,
67
+ resourceArn: `arn:aws:execute-api:${stack.region}:${stack.account}:${resource.api.restApiId}/${stack.stage}/${props.httpMethod}${resource.path}`
68
+ };
69
+ }
@@ -29,7 +29,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.copyFiles = void 0;
30
30
  const path = __importStar(require("path"));
31
31
  const fse = __importStar(require("fs-extra"));
32
- const duration_1 = __importDefault(require("./duration"));
32
+ const duration_1 = __importDefault(require("../tools/duration"));
33
33
  /**
34
34
  * Copies files from the source directory to the build directory.
35
35
  * @function copyFiles
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * Builds ECMAScript files.
3
3
  * Copies the files from the source directory to the build directory and installs the necessary node modules.
4
- * @function buildEcmascript
4
+ * @function buildEcmaScript
5
5
  * @param {string[]} files - The files to build.
6
6
  * @param {string} srcDir - The source directory.
7
7
  * @param {string} buildDir - The build directory.
8
8
  */
9
- export declare function buildEcmascript(files: string[], srcDir: string, buildDir: string): void;
9
+ export declare function buildEcmaScript(files: string[], srcDir: string, buildDir: string): void;
@@ -1,20 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildEcmascript = void 0;
4
- const copy_files_1 = require("./copy-files");
3
+ exports.buildEcmaScript = void 0;
4
+ const copy_files_1 = require("../copy-files");
5
5
  const install_node_modules_1 = require("./install-node-modules");
6
6
  /**
7
7
  * Builds ECMAScript files.
8
8
  * Copies the files from the source directory to the build directory and installs the necessary node modules.
9
- * @function buildEcmascript
9
+ * @function buildEcmaScript
10
10
  * @param {string[]} files - The files to build.
11
11
  * @param {string} srcDir - The source directory.
12
12
  * @param {string} buildDir - The build directory.
13
13
  */
14
- function buildEcmascript(files, srcDir, buildDir) {
14
+ function buildEcmaScript(files, srcDir, buildDir) {
15
15
  // Copies the files from the source directory to the build directory
16
16
  (0, copy_files_1.copyFiles)(files, srcDir, buildDir);
17
17
  // Installs the necessary node modules
18
18
  (0, install_node_modules_1.installNodeModules)(buildDir, ['dev', 'optional', 'peer']);
19
19
  }
20
- exports.buildEcmascript = buildEcmascript;
20
+ exports.buildEcmaScript = buildEcmaScript;
@@ -1,4 +1,6 @@
1
- import { BuildOptions } from "./BuildOptions";
1
+ export interface BuildOptions {
2
+ subdirectory?: string;
3
+ }
2
4
  /**
3
5
  * Builds the source code for a Node.js project.
4
6
  * @function buildNodeSource