Skip to content

Commit

Permalink
localstack
Browse files Browse the repository at this point in the history
  • Loading branch information
xumoyan committed Aug 20, 2024
1 parent dfe7853 commit 2d23165
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 155 deletions.
117 changes: 27 additions & 90 deletions bin/app.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { ChainId } from '@uniswap/sdk-core'
import * as cdk from 'aws-cdk-lib'
import { CfnOutput, SecretValue, Stack, StackProps, Stage, StageProps } from 'aws-cdk-lib'
import { CfnOutput, Stack, StackProps, Stage, StageProps } from 'aws-cdk-lib'
import * as chatbot from 'aws-cdk-lib/aws-chatbot'
import { BuildEnvironmentVariableType } from 'aws-cdk-lib/aws-codebuild'
import { PipelineNotificationEvents } from 'aws-cdk-lib/aws-codepipeline'
import * as sm from 'aws-cdk-lib/aws-secretsmanager'
import { CodeBuildStep, CodePipeline, CodePipelineSource } from 'aws-cdk-lib/pipelines'
import { Construct } from 'constructs'
import dotenv from 'dotenv'
Expand Down Expand Up @@ -80,22 +79,12 @@ export class RoutingAPIPipeline extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props)

const code = CodePipelineSource.gitHub('Uniswap/routing-api', 'main', {
authentication: SecretValue.secretsManager('github-token-2'),
})
const code = CodePipelineSource.gitHub('xumoyan/routing-api', 'main')

const synthStep = new CodeBuildStep('Synth', {
input: code,
buildEnvironment: {
environmentVariables: {
NPM_TOKEN: {
value: 'npm-private-repo-access-token',
type: BuildEnvironmentVariableType.SECRETS_MANAGER,
},
},
},
commands: [
'echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc && npm ci',
'npm ci',
'npm run build',
'npx cdk synth',
],
Expand All @@ -108,62 +97,11 @@ export class RoutingAPIPipeline extends Stack {
synth: synthStep,
})

// Secrets are stored in secrets manager in the pipeline account. Accounts we deploy to
// have been granted permissions to access secrets via resource policies.

const jsonRpcProvidersSecret = sm.Secret.fromSecretAttributes(this, 'RPCProviderUrls', {
// The main secrets use our Infura RPC urls
secretCompleteArn:
'arn:aws:secretsmanager:us-east-2:644039819003:secret:routing-api-rpc-urls-json-primary-ixS8mw',

/*
The backup secrets mostly use our Alchemy RPC urls
However Alchemy does not support Rinkeby, Ropsten, and Kovan
So those chains are set to our Infura RPC urls
When switching to the backups,
we must set the multicall chunk size to 50 so that optimism
does not bug out on Alchemy's end
*/
//secretCompleteArn: arn:aws:secretsmanager:us-east-2:644039819003:secret:routing-api-rpc-urls-json-backup-D2sWoe
})

// Secret that controls the access to the debugging query string params
const unicornSecrets = sm.Secret.fromSecretAttributes(this, 'DebugConfigUnicornSecrets', {
secretCompleteArn: 'arn:aws:secretsmanager:us-east-2:644039819003:secret:debug-config-unicornsecrets-jvmCsq',
})

const tenderlyCreds = sm.Secret.fromSecretAttributes(this, 'TenderlyCreds', {
secretCompleteArn: 'arn:aws:secretsmanager:us-east-2:644039819003:secret:tenderly-api-wQaI2R',
})

const ethGasStationInfoUrl = sm.Secret.fromSecretAttributes(this, 'ETHGasStationUrl', {
secretCompleteArn: 'arn:aws:secretsmanager:us-east-2:644039819003:secret:eth-gas-station-info-url-ulGncX',
})

const pinataApi = sm.Secret.fromSecretAttributes(this, 'PinataAPI', {
secretCompleteArn: 'arn:aws:secretsmanager:us-east-2:644039819003:secret:pinata-api-key-UVLAfM',
})
const route53Arn = sm.Secret.fromSecretAttributes(this, 'Route53Arn', {
secretCompleteArn: 'arn:aws:secretsmanager:us-east-2:644039819003:secret:Route53Arn-elRmmw',
})

const pinataSecret = sm.Secret.fromSecretAttributes(this, 'PinataSecret', {
secretCompleteArn: 'arn:aws:secretsmanager:us-east-2:644039819003:secret:pinata-secret-svGaPt',
})

const hostedZone = sm.Secret.fromSecretAttributes(this, 'HostedZone', {
secretCompleteArn: 'arn:aws:secretsmanager:us-east-2:644039819003:secret:hosted-zone-JmPDNV',
})

const internalApiKey = sm.Secret.fromSecretAttributes(this, 'internal-api-key', {
secretCompleteArn: 'arn:aws:secretsmanager:us-east-2:644039819003:secret:routing-api-internal-api-key-Z68NmB',
})

// Load RPC provider URLs from AWS secret
let jsonRpcProviders = {} as { [chainId: string]: string }
SUPPORTED_CHAINS.forEach((chainId: ChainId) => {
const key = `WEB3_RPC_${chainId}`
jsonRpcProviders[key] = jsonRpcProvidersSecret.secretValueFromJson(key).toString()
jsonRpcProviders[key] = "https://morning-alien-card.quiknode.pro/54d7a389bc802b3e771e92a514d961ddcd9c349a"
new CfnOutput(this, key, {
value: jsonRpcProviders[key],
})
Expand Down Expand Up @@ -203,28 +141,27 @@ export class RoutingAPIPipeline extends Stack {
'ALCHEMY_42161',
]
for (const provider of RPC_GATEWAY_PROVIDERS) {
jsonRpcProviders[provider] = jsonRpcProvidersSecret.secretValueFromJson(provider).toString()
jsonRpcProviders[provider] = "https://morning-alien-card.quiknode.pro/54d7a389bc802b3e771e92a514d961ddcd9c349a"
new CfnOutput(this, provider, {
value: jsonRpcProviders[provider],
})
}

// Beta us-east-2
const betaUsEast2Stage = new RoutingAPIStage(this, 'beta-us-east-2', {
env: { account: '145079444317', region: 'us-east-2' },
env: { account: '000000000000', region: 'us-east-2' },
jsonRpcProviders: jsonRpcProviders,
internalApiKey: internalApiKey.secretValue.toString(),
provisionedConcurrency: 10,
ethGasStationInfoUrl: ethGasStationInfoUrl.secretValue.toString(),
ethGasStationInfoUrl: '',
stage: STAGE.BETA,
route53Arn: route53Arn.secretValueFromJson('arn').toString(),
pinata_key: pinataApi.secretValueFromJson('pinata-api-key').toString(),
pinata_secret: pinataSecret.secretValueFromJson('secret').toString(),
hosted_zone: hostedZone.secretValueFromJson('zone').toString(),
tenderlyUser: tenderlyCreds.secretValueFromJson('tenderly-user').toString(),
tenderlyProject: tenderlyCreds.secretValueFromJson('tenderly-project').toString(),
tenderlyAccessKey: tenderlyCreds.secretValueFromJson('tenderly-access-key').toString(),
unicornSecret: unicornSecrets.secretValueFromJson('debug-config-unicorn-key').toString(),
route53Arn: "",
pinata_key: "",
pinata_secret: "",
hosted_zone: "",
tenderlyUser: "",
tenderlyProject: "",
tenderlyAccessKey: "",
unicornSecret: "",
})

const betaUsEast2AppStage = pipeline.addStage(betaUsEast2Stage)
Expand All @@ -233,21 +170,21 @@ export class RoutingAPIPipeline extends Stack {

// Prod us-east-2
const prodUsEast2Stage = new RoutingAPIStage(this, 'prod-us-east-2', {
env: { account: '606857263320', region: 'us-east-2' },
env: { account: '000000000000', region: 'us-east-2' },
jsonRpcProviders: jsonRpcProviders,
internalApiKey: internalApiKey.secretValue.toString(),
internalApiKey: '',
provisionedConcurrency: 70,
ethGasStationInfoUrl: ethGasStationInfoUrl.secretValue.toString(),
ethGasStationInfoUrl: '',
chatbotSNSArn: 'arn:aws:sns:us-east-2:644039819003:SlackChatbotTopic',
stage: STAGE.PROD,
route53Arn: route53Arn.secretValueFromJson('arn').toString(),
pinata_key: pinataApi.secretValueFromJson('pinata-api-key').toString(),
pinata_secret: pinataSecret.secretValueFromJson('secret').toString(),
hosted_zone: hostedZone.secretValueFromJson('zone').toString(),
tenderlyUser: tenderlyCreds.secretValueFromJson('tenderly-user').toString(),
tenderlyProject: tenderlyCreds.secretValueFromJson('tenderly-project').toString(),
tenderlyAccessKey: tenderlyCreds.secretValueFromJson('tenderly-access-key').toString(),
unicornSecret: unicornSecrets.secretValueFromJson('debug-config-unicorn-key').toString(),
route53Arn: '',
pinata_key: '',
pinata_secret: '',
hosted_zone: '',
tenderlyUser: '',
tenderlyProject: '',
tenderlyAccessKey: '',
unicornSecret: '',
})

const prodUsEast2AppStage = pipeline.addStage(prodUsEast2Stage)
Expand Down Expand Up @@ -372,5 +309,5 @@ new RoutingAPIStack(app, 'RoutingAPIStack', {
})

new RoutingAPIPipeline(app, 'RoutingAPIPipelineStack', {
env: { account: '644039819003', region: 'us-east-2' },
env: { account: '000000000000', region: 'us-east-2' },
})
16 changes: 5 additions & 11 deletions bin/stacks/routing-api-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import * as aws_waf from 'aws-cdk-lib/aws-wafv2'
import { Construct } from 'constructs'
import { STAGE } from '../../lib/util/stage'
import { RoutingCachingStack } from './routing-caching-stack'
import { RoutingDashboardStack } from './routing-dashboard-stack'
import { RoutingLambdaStack } from './routing-lambda-stack'
import { RoutingDatabaseStack } from './routing-database-stack'
import { RpcGatewayDashboardStack } from './rpc-gateway-dashboard'
Expand Down Expand Up @@ -71,9 +70,7 @@ export class RoutingAPIStack extends cdk.Stack {
poolCacheBucket,
poolCacheBucket2,
poolCacheKey,
poolCacheLambdaNameArray,
tokenListCacheBucket,
ipfsPoolCachingLambda,
} = new RoutingCachingStack(this, 'RoutingCachingStack', {
chatbotSNSArn,
stage,
Expand All @@ -94,7 +91,7 @@ export class RoutingAPIStack extends cdk.Stack {
rpcProviderStateDynamoDb,
} = new RoutingDatabaseStack(this, 'RoutingDatabaseStack', {})

const { routingLambda, routingLambdaAlias } = new RoutingLambdaStack(this, 'RoutingLambdaStack', {
const { routingLambdaAlias } = new RoutingLambdaStack(this, 'RoutingLambdaStack', {
poolCacheBucket,
poolCacheBucket2,
poolCacheKey,
Expand Down Expand Up @@ -141,6 +138,10 @@ export class RoutingAPIStack extends cdk.Stack {
allowOrigins: aws_apigateway.Cors.ALL_ORIGINS,
allowMethods: aws_apigateway.Cors.ALL_METHODS,
},
endpointConfiguration: {
types: [aws_apigateway.EndpointType.REGIONAL],
vpcEndpoints: [],
}
})

const ipThrottlingACL = new aws_waf.CfnWebACL(this, 'RoutingAPIIPThrottlingACL', {
Expand Down Expand Up @@ -222,13 +223,6 @@ export class RoutingAPIStack extends cdk.Stack {
webAclArn: ipThrottlingACL.getAtt('Arn').toString(),
})

new RoutingDashboardStack(this, 'RoutingDashboardStack', {
apiName: api.restApiName,
routingLambdaName: routingLambda.functionName,
poolCacheLambdaNameArray,
ipfsPoolCacheLambdaName: ipfsPoolCachingLambda ? ipfsPoolCachingLambda.functionName : undefined,
})

new RpcGatewayDashboardStack(this, 'RpcGatewayDashboardStack')

const lambdaIntegration = new aws_apigateway.LambdaIntegration(routingLambdaAlias)
Expand Down
29 changes: 4 additions & 25 deletions bin/stacks/routing-caching-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ export class RoutingCachingStack extends cdk.NestedStack {
const chatBotTopic = chatbotSNSArn ? aws_sns.Topic.fromTopicArn(this, 'ChatbotTopic', chatbotSNSArn) : undefined

// TODO: Remove and swap to the new bucket below. Kept around for the rollout, but all requests will go to bucket 2.
this.poolCacheBucket = new aws_s3.Bucket(this, 'PoolCacheBucket')
this.poolCacheBucket = new aws_s3.Bucket(this, 'PoolCacheBucket', {

})
this.poolCacheBucket2 = new aws_s3.Bucket(this, 'PoolCacheBucket2')

this.poolCacheBucket2.addLifecycleRule({
Expand Down Expand Up @@ -82,14 +84,6 @@ export class RoutingCachingStack extends cdk.NestedStack {
)
}

const region = cdk.Stack.of(this).region

const lambdaLayerVersion = aws_lambda.LayerVersion.fromLayerVersionArn(
this,
'InsightsLayerPools',
`arn:aws:lambda:${region}:580247275435:layer:LambdaInsightsExtension:14`
)

// Spin up a new pool cache lambda for each config in chain X protocol
for (let i = 0; i < chainProtocols.length; i++) {
const { protocol, chainId, timeout } = chainProtocols[i]
Expand All @@ -108,7 +102,7 @@ export class RoutingCachingStack extends cdk.NestedStack {
sourceMap: true,
},
description: `Pool Cache Lambda for Chain with ChainId ${chainId} and Protocol ${protocol}`,
layers: [lambdaLayerVersion],
layers: [],
tracing: aws_lambda.Tracing.ACTIVE,
environment: {
POOL_CACHE_BUCKET: this.poolCacheBucket.bucketName,
Expand Down Expand Up @@ -179,11 +173,6 @@ export class RoutingCachingStack extends cdk.NestedStack {
},
description: 'IPFS Pool Cache Lambda',
layers: [
aws_lambda.LayerVersion.fromLayerVersionArn(
this,
'InsightsLayerPoolsIPFS',
`arn:aws:lambda:${region}:580247275435:layer:LambdaInsightsExtension:14`
),
],
tracing: aws_lambda.Tracing.ACTIVE,
environment: {
Expand Down Expand Up @@ -214,11 +203,6 @@ export class RoutingCachingStack extends cdk.NestedStack {
},
description: 'Clean IPFS Pool Cache Lambda',
layers: [
aws_lambda.LayerVersion.fromLayerVersionArn(
this,
'InsightsLayerPoolsCleanIPFS',
`arn:aws:lambda:${region}:580247275435:layer:LambdaInsightsExtension:14`
),
],
tracing: aws_lambda.Tracing.ACTIVE,
environment: {
Expand Down Expand Up @@ -264,11 +248,6 @@ export class RoutingCachingStack extends cdk.NestedStack {
sourceMap: true,
},
layers: [
aws_lambda.LayerVersion.fromLayerVersionArn(
this,
'InsightsLayerTokenList',
`arn:aws:lambda:${region}:580247275435:layer:LambdaInsightsExtension:14`
),
],
description: 'Token List Cache Lambda',
tracing: aws_lambda.Tracing.ACTIVE,
Expand Down
2 changes: 1 addition & 1 deletion bin/stacks/routing-database-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as aws_dynamodb from 'aws-cdk-lib/aws-dynamodb'
import { AttributeType, BillingMode } from 'aws-cdk-lib/aws-dynamodb'
import { Construct } from 'constructs'

export interface RoutingDatabaseStackProps extends cdk.NestedStackProps {}
export interface RoutingDatabaseStackProps extends cdk.NestedStackProps { }

export const DynamoDBTableProps = {
RoutesDbTable: {
Expand Down
11 changes: 2 additions & 9 deletions bin/stacks/routing-lambda-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface RoutingLambdaStackProps extends cdk.NestedStackProps {
rpcProviderStateDynamoDb: aws_dynamodb.Table
unicornSecret: string
}

export class RoutingLambdaStack extends cdk.NestedStack {
public readonly routingLambda: aws_lambda_nodejs.NodejsFunction
public readonly routingLambdaAlias: aws_lambda.Alias
Expand Down Expand Up @@ -90,8 +91,6 @@ export class RoutingLambdaStack extends cdk.NestedStack {
tokenPropertiesCachingDynamoDb.grantReadWriteData(lambdaRole)
rpcProviderStateDynamoDb.grantReadWriteData(lambdaRole)

const region = cdk.Stack.of(this).region

this.routingLambda = new aws_lambda_nodejs.NodejsFunction(this, 'RoutingLambda2', {
role: lambdaRole,
runtime: aws_lambda.Runtime.NODEJS_18_X,
Expand Down Expand Up @@ -142,13 +141,7 @@ export class RoutingLambdaStack extends cdk.NestedStack {
UNICORN_SECRET: unicornSecret,
...jsonRpcProviders,
},
layers: [
aws_lambda.LayerVersion.fromLayerVersionArn(
this,
'InsightsLayer',
`arn:aws:lambda:${region}:580247275435:layer:LambdaInsightsExtension:14`
),
],
layers: [],
tracing: aws_lambda.Tracing.ACTIVE,
logRetention: RetentionDays.TWO_WEEKS,
})
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
- "4566:4566"
- "4571:4571"
environment:
- SERVICES=dynamodb,s3,lambda,sns
- SERVICES=dynamodb,apigateway,lambda,rds,ssm,s3,cloudformation,iam,events,cloudwatch
- DEBUG=1
- DATA_DIR=/tmp/localstack_data
volumes:
Expand Down
Loading

0 comments on commit 2d23165

Please sign in to comment.