I am trying to push an image to ECR and use the im...
# aws
k
I am trying to push an image to ECR and use the image for a fargate service. My project structure is like so
Copy code
+ infra/
  ++ ecr/
   +++ index.js <-- ecr.Repository & ecs.Image.fromPath
  ++ ecs/
   +++ index.js <-- ecs.FargateService
  ++ index.js <-- main entry point
+ src/
+ Dockerfile
Copy code
infra/ecr/index.js

var pulumi = require("@pulumi/pulumi");
var awsx = require("@pulumi/awsx");

var ecrRepo = new awsx.ecr.Repository("core", {
  tags: {
    Name: `core ${pulumi.getStack()} repository`,
  },
});
var ecrRepoUrl = ecrRepo.repository.repositoryUrl;

var dockerImage = awsx.ecs.Image.fromPath(ecrRepo, "../../");

module.exports = { ecrRepoUrl, dockerImage };
Copy code
infra/ecs/index.js

var awsx = require("@pulumi/awsx");

var { loadBalancerListener, securityGroup, vpc } = require("../network");
var { atlasCluster } = require("../mongodb");
var { dockerImage } = require("../ecr");

var { getEnvironmentVariables } = require("./helpers");

var ecsCluster = new awsx.ecs.Cluster("core-cluster", {
  vpc,
  securityGroups: [securityGroup],
});

var fargateService = new awsx.ecs.FargateService("core-fgs", {
  ecsCluster,
  taskDefinitionArgs: {
    container: {
      image: dockerImage,
      cpu: 202,
      memory: 1024,
      portMappings: [loadBalancerListener],
      healthCheck: {
        command: ["CMD-SHELL", "curl -f <http://localhost:3000/> || exit 1"],
        startPeriod: 10,
        retries: 3,
      },
      environment: [
        ...getEnvironmentVariables(),
        {
          name: "DB_CONNECTION_URL",
          value:
            process.env.DB_CONNECTION_URL ||
            atlasCluster.connectionStrings.standardSrv,
        },
      ],
    },
  },
  desiredCount: 1,
});

module.exports = { ecsCluster, fargateService };
Copy code
infra/index.js

var { ecrRepoUrl, dockerImage } = require("./ecr");
var { fargateService } = require("./ecs");

module.exports = {
  ecrRepositoryUrl: ecrRepoUrl,
  image: dockerImage.environment,
  fargateService: fargateService.urn,
};
When running pulumi I get the following error
f
Thanks for reporting this. I’ll try to see if I can reproduce with your code.
The problem is this line:
Copy code
var dockerImage = awsx.ecs.Image.fromPath(ecrRepo, "../../");
ecrRepo
should be an
aws.ecr.Repository
and not
awsx.ecr.Repository
(this is slightly confusing, but becomes more clear if this example is run under TypeScript since the type-checking will fail). If you pass in
ecrRepo.repistory
does it work for you?
k
Life saver! That made it work. Could you please explain a bit of the difference between the awsx and aws when it comes to creating an ECR Repository?
f
awsx
is a higher-level component library that abstracts away some of the complexities of working with the ‘raw’ AWS resources in
aws
.
awsx
makes various opinionated defaults about how to set things up so you don’t have to think about it. In this case, the
ecs
part of
awsx
was built so that it could interop with any repository (awsx or not), which is why it takes an
aws.ecr.Repository
— probably what could be done to make this even easier is to accept an
awsx.ecr.Repository
object as well and if it gets one of those, just grab the
repository
property from that object. Feel free to file an issue if you think that is a worthwhile ergonomic change.
1