Skip to content

Commit

Permalink
test with dummy value as SSM parameter is unresolved until deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Park committed Nov 15, 2023
1 parent 290e602 commit 43af29c
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/prerender-fargate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
PrerenderFargateRecachingOptions,
PrerenderFargateScalingOptions,
} from "./lib/prerender-fargate-options";
import { PrerenderTokenUrlAssociationOptions } from "./lib/recaching/prerender-tokens";
import { PrerenderTokenUrlAssociationOptions } from "./lib/generateTokensUrlAssociation";

export {
PrerenderFargate,
Expand Down
36 changes: 29 additions & 7 deletions packages/prerender-fargate/lib/prerender-fargate-options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PrerenderTokenUrlAssociationOptions } from "./recaching/prerender-tokens";
import { PrerenderTokenUrlAssociationOptions } from "./generateTokensUrlAssociation";

/**
* Options for configuring the Prerender Fargate construct.
Expand Down Expand Up @@ -27,10 +27,10 @@ export interface PrerenderFargateOptions {
/**
* A list of tokens to use for authentication with the Prerender service.
* This parameter is deprecated and will be removed in a future release.
* Please use the `tokenUrlAssociation` parameter instead.
* *If `tokenUrlAssociation` is provided, `tokenList` will be ignored*
* Please use the `tokenUrlSSMParamPrefix` or `tokenUrlAssociation` parameter instead.
* *If either `tokenUrlSSMParamPrefix` or `tokenUrlAssociation` is provided, `tokenList` will be ignored.
*/
tokenList: Array<string>;
tokenList?: Array<string>;
/**
* The ARN of the SSL certificate to use for HTTPS connections.
*/
Expand Down Expand Up @@ -64,9 +64,31 @@ export interface PrerenderFargateOptions {
*/
enableS3Endpoint?: boolean;
/**
* Configuration for associating tokens with specific domain URLs.
* During the reacaching process, these tokens will be used to validate the request.
* ### Example:
* Read Prerender token-URL associations from a pre-defined AWS SSM Parameter, which is the the preferred way of token-URL association.
* Mutually exclusive with tokenUrlSSMParameter.
* Either one of tokenList, tokenUrlSSMParameter, or tokenUrlAssociation must be specified.
* ### SSM Parameter Example:
* ```
* {
* tokenUrlAssociation: {
* token1: [
* "https://example.com",
* "https://acme.example.com"],
* token2: [
* "https://example1.com",
* "https://acme.example1.com"]
* },
* ssmPathPrefix: "/prerender/recache/tokens"
* }
* ```
* This results in generate two AWS SSM Parameters that associates tokens with specific domain URLs for domain validation.
*/
tokenUrlSSMParameter?: string;
/**
* Generate AWS SSM Parameters that associates tokens with specific domain URLs for domain validation.
* Mutually exclusive with tokenUrlSSMParameter.
* Either one of tokenList, tokenUrlSSMParameter, or tokenUrlAssociation must be specified.
* ### Typescript Example:
* ```typescript
* {
* tokenUrlAssociation: {
Expand Down
62 changes: 52 additions & 10 deletions packages/prerender-fargate/lib/prerender-fargate.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Construct } from "constructs";
import * as ecs from "aws-cdk-lib/aws-ecs";
import * as ssm from "aws-cdk-lib/aws-ssm";
import * as ecsPatterns from "aws-cdk-lib/aws-ecs-patterns";
import * as ec2 from "aws-cdk-lib/aws-ec2";
import { Certificate } from "aws-cdk-lib/aws-certificatemanager";
import { HostedZone } from "aws-cdk-lib/aws-route53";
import { Bucket, BlockPublicAccess } from "aws-cdk-lib/aws-s3";
import * as ecrAssets from "aws-cdk-lib/aws-ecr-assets";
import { Duration, RemovalPolicy, Stack } from "aws-cdk-lib";
import { Duration, RemovalPolicy, Stack, Lazy } from "aws-cdk-lib";
import * as path from "path";
import { PrerenderTokenUrlAssociation } from "./recaching/prerender-tokens";
import { PrerenderTokenUrlAssociation } from "./generateTokensUrlAssociation";
import { PrerenderRecacheApi } from "./recaching/prerender-recache-api-construct";
import { PrerenderFargateOptions } from "./prerender-fargate-options";

Expand Down Expand Up @@ -50,7 +51,6 @@ import { PrerenderFargateOptions } from "./prerender-fargate-options";
* maxInstanceCount: 2,
* enableS3Endpoint: true,
* tokenUrlAssociation: {
* tokenUrlAssociation: {
* token1: [
* "https://example.com",
* "https://acme.example.com"
Expand Down Expand Up @@ -85,7 +85,7 @@ export class PrerenderFargate extends Construct {
super(scope, id);

const {
tokenUrlAssociation,
tokenUrlSSMParameter,
certificateArn,
vpcId,
maxInstanceCount,
Expand All @@ -102,6 +102,7 @@ export class PrerenderFargate extends Construct {
prerenderFargateScalingOptions,
prerenderFargateRecachingOptions,
} = props;
let { tokenUrlAssociation } = props;

// Create bucket for prerender storage
this.bucket = new Bucket(this, `${prerenderName}-bucket`, {
Expand Down Expand Up @@ -134,12 +135,53 @@ export class PrerenderFargate extends Construct {
);

/**
* This provide backward compatibility for the tokenList property
* If tokenUrlAssociation is provided, tokenList will be ignored
* This provide backward compatibility for the tokenList property.
* If tokenUrlSSMParamPrefix or tokenUrlAssociation is provided, tokenList will be ignored
*/
const tokenList = tokenUrlAssociation
? Object.keys(tokenUrlAssociation.tokenUrlAssociation)
: props.tokenList.toString();

let tokenList;

if (tokenUrlSSMParameter) {
////
let paramValue = ssm.StringParameter.valueForStringParameter(
this,
tokenUrlSSMParameter
);
// if (paramValue.startsWith("dummy-value")) return;

const dummyParam = `
{
"tokenUrlAssociation": {
"token1": [ "https://dummy1.com"],
"token2": [ "https://dummy2.com"]
},
"ssmPathPrefix": "/prerender/recache/tokens"
}
`;
if (paramValue[0] === "$") paramValue = dummyParam;

console.log("#################");
// console.log(Lazy.string({ produce: () => paramValue }));
console.log(paramValue);
console.log("#################");
////
const tokenUrlSSMParameterValue = JSON.parse(
// ssm.StringParameter.valueFromLookup(this, tokenUrlSSMParameter)
// Use valueForStringParameter (read at deployment time) vs valueFromLookup(read at synthesis time with help of context) to keep the value
paramValue
);
tokenUrlAssociation = tokenUrlSSMParameterValue;
tokenList = Object.keys(tokenUrlSSMParameterValue.tokenUrlAssociation);
} else if (tokenUrlAssociation) {
tokenList = Object.keys(tokenUrlAssociation.tokenUrlAssociation);
} else if (props.tokenList) {
tokenList = props.tokenList;
} else {
throw new Error(
"Either one of tokenUrlSSMParameter, tokenUrlAssociation, or tokenList must be provided."
);
}
// TO-DO: let PrerenderTokenUrlAssociation stack be created. Currently the main stack doesn't create the parameters.

// Create a load-balanced Fargate service
const fargateService =
Expand Down Expand Up @@ -219,7 +261,7 @@ export class PrerenderFargate extends Construct {

/**
* Enable VPC Endpoints for S3
* This would create S3 endpoints in all the PUBLIC subnets of the VPC
* This would create S3 endpoints in all the PUBLIC subnets of the VPC
*/
if (enableS3Endpoint) {
vpc.addGatewayEndpoint("S3Endpoint", {
Expand Down
2 changes: 1 addition & 1 deletion packages/prerender-fargate/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aligent/cdk-prerender-fargate",
"version": "0.0.1",
"version": "2.1.0",
"description": "A construct to host Prerender in Fargate",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 43af29c

Please sign in to comment.