Skip to content

Commit

Permalink
Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
fwang committed Nov 22, 2023
1 parent bae88b1 commit bfc127b
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 33 deletions.
1 change: 1 addition & 0 deletions internal/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"$schema": "https://json.schemastore.org/package.json",
"name": "sst",
"dependencies": {
"@aws-sdk/client-lambda": "^3.454.0",
"@aws-sdk/client-sts": "^3.454.0",
"@pulumi/aws": "5.43.0",
"@pulumi/pulumi": "3.94.2"
Expand Down
99 changes: 99 additions & 0 deletions internal/components/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 52 additions & 13 deletions internal/components/src/components/function-code-updater.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
import { STSClient, GetCallerIdentityCommand } from "@aws-sdk/client-sts";
import pulumi from "@pulumi/pulumi";
import {
LambdaClient,
UpdateFunctionCodeCommand,
} from "@aws-sdk/client-lambda";

class Provider implements util.dynamic.ResourceProvider {
async create(): Promise<util.dynamic.CreateResult> {
const client = new STSClient();
const identity = await client.send(new GetCallerIdentityCommand({}));
return { id: "foo", outs: { account: identity.Account } };
}
export interface FunctionCodeUpdaterInputs {
s3Bucket: pulumi.Input<string>;
s3Key: pulumi.Input<string>;
functionName: pulumi.Input<string>;
}

interface Inputs {
s3Bucket: string;
s3Key: string;
functionName: string;
}

export class FunctionCodeUpdater extends util.dynamic.Resource {
account!: util.Output<string>;
constructor(name: string) {
super(new Provider(), name, { account: undefined });
class Provider implements pulumi.dynamic.ResourceProvider {
async create(inputs: Inputs): Promise<pulumi.dynamic.CreateResult> {
const client = new LambdaClient();
await client.send(
new UpdateFunctionCodeCommand({
FunctionName: inputs.functionName,
S3Bucket: inputs.s3Bucket,
S3Key: inputs.s3Key,
})
);
return { id: "foo" };
}

async update(
id: string,
olds: Inputs,
news: Inputs
): Promise<pulumi.dynamic.UpdateResult> {
const client = new LambdaClient();
await client.send(
new UpdateFunctionCodeCommand({
FunctionName: news.functionName,
S3Bucket: news.s3Bucket,
S3Key: news.s3Key,
})
);
return {};
}
}

//const updater = new FunctionCodeUpdater("foo");
//export const account = updater.account;
export class FunctionCodeUpdater extends pulumi.dynamic.Resource {
account!: pulumi.Output<string>;
constructor(
name: string,
args: FunctionCodeUpdaterInputs,
opts?: pulumi.CustomResourceOptions
) {
super(new Provider(), name, args, opts);
}
}
24 changes: 12 additions & 12 deletions internal/components/src/components/function.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pulumi from "@pulumi/pulumi";
import { FunctionCodeUpdater } from "./function-code-updater";

export interface FunctionArgs
Expand All @@ -10,12 +11,12 @@ export interface FunctionArgs
policies: aws.types.input.iam.RoleInlinePolicy[];
}

export class Function extends util.ComponentResource {
public readonly name: util.Output<string>;
export class Function extends pulumi.ComponentResource {
public readonly name: pulumi.Output<string>;
constructor(
name: string,
args: FunctionArgs,
opts?: util.ComponentResourceOptions
opts?: pulumi.ComponentResourceOptions
) {
super("sst:sst:Function", name, args, opts);

Expand All @@ -40,28 +41,27 @@ export class Function extends util.ComponentResource {
{
key: `${name}-code-${args.bundleHash}.zip`,
bucket: app.bootstrap.bucket,
source: new util.asset.FileArchive(bundle),
source: new pulumi.asset.FileArchive(bundle),
},
{ parent: this }
);
const fn = new aws.lambda.Function(
`${name}-function`,
{
code: new util.asset.AssetArchive({
index: new util.asset.StringAsset("exports.handler = () => {}"),
code: new pulumi.asset.AssetArchive({
index: new pulumi.asset.StringAsset("exports.handler = () => {}"),
}),
role: role.arn,
...args,
},
{ parent: this }
);

new FunctionCodeUpdater(`${name}-code-updater`);
// new FunctionCodeUpdater(`${name}-code-updater`, {
// functionName: fn.name,
// s3Bucket: file.bucket,
// s3Key: file.key,
// });
new FunctionCodeUpdater(`${name}-code-updater`, {
functionName: fn.name,
s3Bucket: file.bucket,
s3Key: file.key,
});

this.name = fn.name;
}
Expand Down
17 changes: 9 additions & 8 deletions internal/components/src/components/ssr-site.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import * as fs from "fs";
import * as path from "path";
import { execSync } from "child_process";
import pulumi from "@pulumi/pulumi";
import { Function } from "./function";

export interface SsrSiteArgs extends util.ComponentResourceOptions {
export interface SsrSiteArgs extends pulumi.ComponentResourceOptions {
path: string;
}

export class SsrSite extends util.ComponentResource {
export class SsrSite extends pulumi.ComponentResource {
public readonly distribution: aws.cloudfront.Distribution;
constructor(name: string, args: SsrSiteArgs) {
super("sst:sst:SsrSite", name, args);
Expand Down Expand Up @@ -74,7 +75,7 @@ export class SsrSite extends util.ComponentResource {

new aws.s3.BucketObject(itemPath, {
bucket: bucket.bucket,
source: new util.asset.FileAsset(filePath), // use FileAsset to point to a file
source: new pulumi.asset.FileAsset(filePath), // use FileAsset to point to a file
contentType: getContentType(filePath, "UTF-8"),
cacheControl:
"public,max-age=0,s-maxage=86400,stale-while-revalidate=8640",
Expand Down Expand Up @@ -327,7 +328,7 @@ export class SsrSite extends util.ComponentResource {

function createDistributionInvalidation() {
//new command.local.Command("invalidate", {
// create: util.interpolate`aws cloudfront create-invalidation --distribution-id ${distribution.id} --paths index.html`
// create: pulumi.interpolate`aws cloudfront create-invalidation --distribution-id ${distribution.id} --paths index.html`
// environment: {
// ETAG: indexFile.etag
// }
Expand Down Expand Up @@ -355,11 +356,11 @@ export class SsrSite extends util.ComponentResource {
principals: [
{
type: "AWS",
identifiers: [util.interpolate`${access.iamArn}`],
identifiers: [pulumi.interpolate`${access.iamArn}`],
},
],
actions: ["s3:GetObject"],
resources: [util.interpolate`${bucket.arn}/*`],
resources: [pulumi.interpolate`${bucket.arn}/*`],
},
],
});
Expand Down Expand Up @@ -440,7 +441,7 @@ export class SsrSite extends util.ComponentResource {
return new aws.lambda.Function(`${name}-image`, {
description: "Next.js server",
handler: "index.handler",
code: new util.asset.FileArchive(
code: new pulumi.asset.FileArchive(
path.join(sitePath, ".open-next", "image-optimization-function")
),
runtime: "nodejs18.x",
Expand Down Expand Up @@ -494,7 +495,7 @@ export class SsrSite extends util.ComponentResource {
`${name}-revalidation-consumer`,
{
handler: "index.handler",
code: new util.asset.FileArchive(
code: new pulumi.asset.FileArchive(
path.join(sitePath, ".open-next", "revalidation-function")
),
runtime: "nodejs18.x",
Expand Down

0 comments on commit bfc127b

Please sign in to comment.