Skip to content

Commit

Permalink
feat(cdk): allow setting port, command, and container registry
Browse files Browse the repository at this point in the history
  • Loading branch information
shellscape committed Jun 15, 2024
1 parent d3c6e2e commit 5dc7de3
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packages/cdk/src/methods/fargate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export interface AddServiceOptions {
assignPublicIp?: boolean;
baseDir: string;
certificateArn: string;
command?: string[];
containerRegistry?: string;
cpu?: ServiceCPUUnits;
cpuScaleAtPercent: MinMaxNumber<10, 90>;
defaultVpc?: boolean;
Expand All @@ -52,6 +54,7 @@ export interface AddServiceOptions {
name?: string;
nodeMemorySize?: number;
os?: OperatingSystemFamily;
port?: number;
scope: DotStack;
vpc?: IVpc;
}
Expand All @@ -68,6 +71,8 @@ export const addFargateService = (options: AddServiceOptions): AddServiceResult
assignPublicIp = true,
baseDir,
certificateArn,
command,
containerRegistry,
cpu = ServiceCPUUnits.HALF_VCPU,
cpuScaleAtPercent = 50,
defaultVpc,
Expand All @@ -79,17 +84,21 @@ export const addFargateService = (options: AddServiceOptions): AddServiceResult
name = '',
nodeMemorySize = 2000,
os = OperatingSystemFamily.LINUX,
port = 80,
scope
} = options;
let { vpc } = options;
const { env } = scope;
const baseName = DotStack.baseName(name, 'service');
const serviceName = scope.resourceName(baseName);
const certificate = Certificate.fromCertificateArn(scope, `${serviceName}-cert`, certificateArn);

const asset = new DockerImageAsset(scope, `${serviceName}-asset`, {
directory: baseDir
});
const image = containerRegistry
? ContainerImage.fromRegistry(containerRegistry)
: ContainerImage.fromDockerImageAsset(
new DockerImageAsset(scope, `${serviceName}-asset`, {
directory: baseDir
})
);

if (defaultVpc) vpc = Vpc.fromLookup(scope, 'Vpc', { isDefault: true });

Expand All @@ -109,7 +118,8 @@ export const addFargateService = (options: AddServiceOptions): AddServiceResult
},
serviceName,
taskImageOptions: {
containerPort: 80,
command,
containerPort: port,
environment: {
AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
DEPLOY_ENV: env,
Expand All @@ -119,7 +129,7 @@ export const addFargateService = (options: AddServiceOptions): AddServiceResult
...environmentVariables
},
family: `${serviceName}-task-def`,
image: ContainerImage.fromDockerImageAsset(asset),
image,
logDriver: LogDriver.awsLogs({
logRetention: RetentionDays.ONE_WEEK,
streamPrefix: serviceName
Expand Down

0 comments on commit 5dc7de3

Please sign in to comment.