export function CreateFargateService()

in source/patterns/@aws-solutions-constructs/core/lib/fargate-helper.ts [21:131]


export function CreateFargateService(
  scope: Construct,
  id: string,
  constructVpc: ec2.IVpc,
  clientClusterProps?: ecs.ClusterProps,
  ecrRepositoryArn?: string,
  ecrImageVersion?: string,
  clientFargateTaskDefinitionProps?: ecs.FargateTaskDefinitionProps | any,
  clientContainerDefinitionProps?: ecs.ContainerDefinitionProps | any,
  clientFargateServiceProps?: ecs.FargateServiceProps | any
): [ecs.FargateService, ecs.ContainerDefinition] {
  defaults.AddAwsServiceEndpoint(
    scope,
    constructVpc,
    defaults.ServiceEndpointTypes.ECR_API
  );
  defaults.AddAwsServiceEndpoint(
    scope,
    constructVpc,
    defaults.ServiceEndpointTypes.ECR_DKR
  );
  defaults.AddAwsServiceEndpoint(
    scope,
    constructVpc,
    defaults.ServiceEndpointTypes.S3
  );

  const constructContainerDefintionProps: any = {};
  const constructFargateServiceDefinitionProps: any = {};

  if (!clientFargateServiceProps?.cluster) {
    // Construct Fargate Service
    constructFargateServiceDefinitionProps.cluster = CreateCluster(
      scope,
      `${id}-cluster`,
      constructVpc,
      clientClusterProps
    );
  }

  // Set up the Fargate service
  if (!clientContainerDefinitionProps?.image) {
    constructContainerDefintionProps.image = CreateImage(
      scope,
      id,
      ecrRepositoryArn,
      ecrImageVersion
    );
  }

  // Create the Fargate Service
  let newContainerDefinition;
  [constructFargateServiceDefinitionProps.taskDefinition, newContainerDefinition] = CreateTaskDefinition(
    scope,
    id,
    clientFargateTaskDefinitionProps,
    clientContainerDefinitionProps,
    constructContainerDefintionProps
  );

  if (!clientFargateServiceProps?.vpcSubnets) {
    if (constructVpc.isolatedSubnets.length) {
      constructFargateServiceDefinitionProps.vpcSubnets = {
        subnets: constructVpc.isolatedSubnets,
      };
    } else {
      constructFargateServiceDefinitionProps.vpcSubnets = {
        subnets: constructVpc.privateSubnets,
      };
    }
  }

  let defaultFargateServiceProps;

  if (!clientFargateServiceProps?.securityGroups) {
    const serviceSecurityGroup = new ec2.SecurityGroup(scope, `${id}-sg`, {
      allowAllOutbound: true,
      disableInlineRules: false,
      vpc: constructVpc,
      // We add a description here so that this SG can be easily identified in tests
      description: 'Construct created security group'
    });
    defaultFargateServiceProps = overrideProps(defaults.DefaultFargateServiceProps(), { securityGroups: [ serviceSecurityGroup ]});
    defaults.addCfnSuppressRules(serviceSecurityGroup, [
      {
        id: 'W5',
        reason: 'Egress of 0.0.0.0/0 is default and generally considered OK',
      },
      {
        id: 'W40',
        reason: 'Egress IPProtocol of -1 is default and generally considered OK',
      }
    ]);
  } else {
    defaultFargateServiceProps = defaults.DefaultFargateServiceProps();
  }

  const fargateServiceProps = defaults.consolidateProps(
    defaultFargateServiceProps,
    clientFargateServiceProps,
    constructFargateServiceDefinitionProps
  );

  const newService = new ecs.FargateService(
    scope,
    `${id}-service`,
    fargateServiceProps,
  );

  return [newService, newContainerDefinition];
}