-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40080fa
commit fa0ff61
Showing
8 changed files
with
184 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Config, type StackContext } from "sst/constructs"; | ||
|
||
/** | ||
* Simple stack for the config | ||
* @param stack | ||
* @constructor | ||
*/ | ||
export function ConfigStack({ stack }: StackContext) { | ||
// RPCs | ||
const rpcSecrets = [ | ||
// BlockPi rpcs | ||
new Config.Secret(stack, "BLOCKPI_API_KEY_ARB_SEPOLIA"), | ||
// Alchemy RPC | ||
new Config.Secret(stack, "ALCHEMY_API_KEY"), | ||
]; | ||
|
||
// Databases | ||
const ponderDb = new Config.Secret(stack, "DATABASE_URL"); | ||
const erpcDb = new Config.Secret(stack, "ERPC_DATABASE_URL"); | ||
|
||
// Return all of that | ||
return { | ||
rpcSecrets, | ||
ponderDb, | ||
erpcDb, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { Repository } from "aws-cdk-lib/aws-ecr"; | ||
import { ContainerImage } from "aws-cdk-lib/aws-ecs"; | ||
import { Service, type StackContext, use } from "sst/constructs"; | ||
import { ConfigStack } from "./Config"; | ||
import { buildSecretsMap } from "./utils"; | ||
|
||
/** | ||
* The CDK stack that will deploy the erpc service | ||
* @param stack | ||
* @constructor | ||
*/ | ||
export function ErpcStack({ app, stack }: StackContext) { | ||
// All the secrets env variable we will be using (in local you can just use a .env file) | ||
const { rpcSecrets, erpcDb } = use(ConfigStack); | ||
const secrets = [...rpcSecrets, erpcDb]; | ||
|
||
// Get our CDK secrets map | ||
const cdkSecretsMap = buildSecretsMap(stack, secrets); | ||
|
||
// Get the container props of our prebuilt binaries | ||
const containerRegistry = Repository.fromRepositoryAttributes( | ||
stack, | ||
"ErpcEcr", | ||
{ | ||
repositoryArn: `arn:aws:ecr:eu-west-1:${app.account}:repository/erpc`, | ||
repositoryName: "erpc", | ||
} | ||
); | ||
|
||
const imageTag = process.env.COMMIT_SHA ?? "latest"; | ||
console.log(`Will use the image ${imageTag}`); | ||
const erpcImage = ContainerImage.fromEcrRepository( | ||
containerRegistry, | ||
imageTag | ||
); | ||
|
||
// The service itself | ||
const erpcService = new Service(stack, "ErpcService", { | ||
path: "packages/erpc", | ||
port: 4000, | ||
// Domain mapping | ||
customDomain: { | ||
domainName: "erpc.frak.id", | ||
hostedZone: "frak.id", | ||
}, | ||
// Setup some capacity options | ||
scaling: { | ||
minContainers: 1, | ||
maxContainers: 1, | ||
cpuUtilization: 90, | ||
memoryUtilization: 90, | ||
}, | ||
// Bind the secret we will be using | ||
bind: secrets, | ||
// Arm architecture (lower cost) | ||
architecture: "arm64", | ||
// Hardware config | ||
cpu: "1 vCPU", | ||
memory: "4 GB", | ||
storage: "30 GB", | ||
// Log retention | ||
logRetention: "one_week", | ||
// Set the right environment variables | ||
environment: { | ||
ERPC_LOG_LEVEL: "warn", | ||
}, | ||
cdk: { | ||
// Customise fargate service to enable circuit breaker (if the new deployment is failing) | ||
fargateService: { | ||
circuitBreaker: { | ||
enable: true, | ||
}, | ||
// Disable rolling update | ||
desiredCount: 1, | ||
minHealthyPercent: 0, | ||
maxHealthyPercent: 100, | ||
}, | ||
// Directly specify the image position in the registry here | ||
container: { | ||
containerName: "erpc", | ||
image: erpcImage, | ||
secrets: cdkSecretsMap, | ||
}, | ||
}, | ||
}); | ||
|
||
stack.addOutputs({ | ||
erpcServiceId: erpcService.id, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Secret } from "aws-cdk-lib/aws-ecs"; | ||
import { StringParameter } from "aws-cdk-lib/aws-ssm"; | ||
import type { Config, Stack } from "sst/constructs"; | ||
|
||
const specificSecretsList = ["ERPC_DATABASE_URL", "DATABASE_URL"]; | ||
|
||
/** | ||
* Build a list of secret name to CDK secret, for direct binding | ||
* @param stack | ||
* @param secrets | ||
*/ | ||
export function buildSecretsMap(stack: Stack, secrets: Config.Secret[]) { | ||
return secrets.reduce( | ||
(acc, secret) => { | ||
const isSpecificSecret = specificSecretsList.includes(secret.name); | ||
const ssmPath = isSpecificSecret | ||
? `/indexer/sst/Secret/${secret.name}/value` | ||
: `/sst/frak-indexer/.fallback/Secret/${secret.name}/value`; | ||
|
||
// Add the secret | ||
const stringParameter = | ||
StringParameter.fromSecureStringParameterAttributes( | ||
stack, | ||
`Secret${secret.name}`, | ||
{ | ||
parameterName: ssmPath, | ||
} | ||
); | ||
acc[secret.name] = Secret.fromSsmParameter(stringParameter); | ||
return acc; | ||
}, | ||
{} as Record<string, Secret> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters