Hello, I still can't get my head around issues wit...
# typescript
a
Hello, I still can't get my head around issues with getting ecr images (
awsx.ecs.Image
) to be used with k8s deployments, however I narrow down issues to this:
Copy code
// generate and push ECR image
const image = awsx.ecr.buildAndPushImage(apiRepoName, config.dockerContextPath, ...);
How can I use that
image
in the k8s.apps.v1.Deployment container spec? It expects string value of path to the image published in some registry and all samples I can found in Pulumi just use some 'nginx' image, not custom build image like above with
buildAndPushImage
method, which returns
awsx.ecs.Image
type object. It's also very confusing how to get any information from
image
object above, e.g. say registry path or even how to "await" such image to be built... (assuming I somehow can interpolate path to use it later). Totally stuck, please HELP 🙂 (more details in https://pulumi-community.slack.com/archives/C84L4E3N1/p1569874384140300)
g
Haven’t tested it myself, but looks like you could use
const image = awsx.ecr.buildAndPushImage(apiRepoName, config.dockerContextPath, ...).imageValue;
to get the image name for k8s. If you need the fully-qualified name, you could do something like
Copy code
const repoImage = awsx.ecr.buildAndPushImage(apiRepoName, config.dockerContextPath, ...);

const fqImage = pulumi.concat(repoImage.repository, ":", repoImage.imageValue);
a
Hi @gorgeous-egg-16927, thanks for an answer, however it's different types:
Copy code
export declare abstract class Image implements ecs.ContainerImageProvider
and
Copy code
export declare class RepositoryImage implements ecs.ContainerImageProvider
awsx.ecr.buildAndPushImage
seems returns instance of some class which inherits abstract
Image
, while you suggest to use properties (
.imageValue
or
.repository
) from
RepositoryImage
(
ContainerImageProvider
does not have such properties defined...)
g
Copy code
const repoImage = awsx.ecr.buildAndPushImage("test", ".");
const appLabels = { app: "test" };
const deployment = new k8s.apps.v1.Deployment("test", {
        spec: {
            selector: { matchLabels: appLabels },
            replicas: 1,
            template: {
                metadata: { labels: appLabels },
                spec: {
                    containers: [{ name: "test", image: repoImage.imageValue}],
                },
            },
        }
    },
);
worked for me. Apparently you just need the
imageValue
property
a
@gorgeous-egg-16927 sorry I confuse you and myself... I am using
Copy code
const apiImage = awsx.ecs.Image.fromDockerBuild(repositoryApi, {
    context: config.dockerContextPath,
    dockerfile: config.dockerAPIFile
  });
i.e.
fromDockerBuild
, not
buildAndPushImage
so, return type is not
awsx.ecr.RepositoryImage
(which has property imageValue), but return type is
awsx.ecs.Image
@gorgeous-egg-16927 thanks for your help, but I was unable to "convert" between 2 of this types, so for now I generate images for usage in k8s with
buildAndPushImage
and for usage in Fargate with `fromDockerBuild`:
Copy code
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";
import * as config from "./config";
import { Environment } from "./environments";

export const createDockerImages = async (environment: Environment) => {
    
  const apiRepoName = `gauzy/api-${environment.toLowerCase()}`;

  const repositoryApi = new aws.ecr.Repository(apiRepoName, {
    name: apiRepoName
  });

  const webappRepoName = `gauzy/webapp-${environment.toLowerCase()}`;

  const repositoryWebapp = new aws.ecr.Repository(webappRepoName, {
    name: webappRepoName
  });

  let apiImage;

  // Build and publish a Docker image to a private ECR registry for API.
  if (environment !== Environment.Prod)
  {  
    apiImage = awsx.ecs.Image.fromDockerBuild(repositoryApi, {
      context: config.dockerContextPath,
      dockerfile: config.dockerAPIFile
    });
  }
  else 
  {
    apiImage = awsx.ecr.buildAndPushImage(
      apiRepoName,
      {
        context: config.dockerContextPath,
        dockerfile: config.dockerAPIFile
      },
      { repository: repositoryApi }
    );
  }

  let webappImage;
  
  // Build and publish a Docker image to a private ECR registry for Web App.
  if (environment !== Environment.Prod)
  {      
    webappImage = awsx.ecs.Image.fromDockerBuild(repositoryWebapp, {
      context: config.dockerContextPath,
      dockerfile: config.dockerWebappFile
    });
  } 
  else 
  {
    webappImage = awsx.ecr.buildAndPushImage(
      webappRepoName,
      {
        context: config.dockerContextPath,
        dockerfile: config.dockerWebappFile
      },
      { repository: repositoryWebapp }
    );
  }

  return { apiImage, webappImage };
};