| 10 | import { Resource } from "sst"; |
| 11 | |
| 12 | export async function handler(event: CloudFormationCustomResourceEvent) { |
| 13 | console.log(event); |
| 14 | let status: CloudFormationCustomResourceResponse["Status"] = "SUCCESS"; |
| 15 | if (event.RequestType === "Create") { |
| 16 | try { |
| 17 | await withActor( |
| 18 | { |
| 19 | type: "system", |
| 20 | properties: { |
| 21 | workspaceID: event.ResourceProperties.workspaceID, |
| 22 | }, |
| 23 | }, |
| 24 | async () => { |
| 25 | const credentials = await AWS.assumeRole( |
| 26 | event.ResourceProperties.accountID, |
| 27 | ); |
| 28 | if (credentials) { |
| 29 | await AWS.Account.create({ |
| 30 | accountID: event.ResourceProperties.accountID, |
| 31 | }); |
| 32 | await Replicache.poke(); |
| 33 | console.log(credentials); |
| 34 | status = "SUCCESS"; |
| 35 | } else { |
| 36 | status = "FAILED"; |
| 37 | } |
| 38 | }, |
| 39 | ); |
| 40 | } catch (ex) { |
| 41 | console.error(ex); |
| 42 | status = "FAILED"; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | if (event.RequestType === "Delete") { |
| 47 | await withActor( |
| 48 | { |
| 49 | type: "system", |
| 50 | properties: { |
| 51 | workspaceID: event.ResourceProperties.workspaceID, |
| 52 | }, |
| 53 | }, |
| 54 | async () => { |
| 55 | const account = await AWS.Account.fromAccountID( |
| 56 | event.ResourceProperties.accountID, |
| 57 | ); |
| 58 | if (!account) return; |
| 59 | |
| 60 | await bus.publish(Resource.Bus, AWS.Account.Events.Removed, { |
| 61 | awsAccountID: account.id, |
| 62 | }); |
| 63 | }, |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | const json: CloudFormationCustomResourceResponse = { |
| 68 | Status: status, |
| 69 | Reason: "", |