https://pulumi.com logo
#getting-started
Title
# getting-started
a

acceptable-plumber-31485

03/14/2023, 2:30 AM
I tried the code from this page yesterday, https://www.pulumi.com/docs/guides/crosswalk/aws/elb/. However, I couldn't get it to work.
pulumi up
was complaining about export. I tried creating it as a typescript as well as javascript. Both failed. It does look like it's a javascript code. Any ideas?
l

little-cartoon-10569

03/14/2023, 2:37 AM
That's typescript. Are you overriding the typescript runtime configuration? There's nothing odd about that example. What error are you getting? And which example are you trying?
a

acceptable-plumber-31485

03/14/2023, 2:47 AM
I'm retrying it now
All I did yesterday was execute
pulumi new aws-typescript
. Then I copied the code from that page and placed it all on index.ts. Then I ran pulumi up
one sec
Here is the output of pulumi preview
Copy code
$ pulumi preview
Previewing update (dev-ts):
     Type                 Name      Plan       Info
 +   pulumi:pulumi:Stack  t-dev-ts  create     1 error


Diagnostics:
  pulumi:pulumi:Stack (t-dev-ts):
    error: Running program '/usr/local/src/repos/IaC/t/' failed with an unhandled exception:
    TSError: ⨯ Unable to compile TypeScript:
    index.ts(53,1): error TS1128: Declaration or statement expected.
    index.ts(5,1): error TS2309: An export assignment cannot be used in a module with other exported elements.
    index.ts(9,28): error TS2339: Property 'getDefault' does not exist on type 'typeof Vpc'.
    index.ts(10,27): error TS2339: Property 'SecurityGroup' does not exist on type 'typeof import("/usr/local/src/repos/IaC/t/node_modules/@pulumi/awsx/ec2/index")'.
    index.ts(21,24): error TS2339: Property 'createListener' does not exist on type 'ApplicationLoadBalancer'.
    index.ts(40,36): error TS2339: Property 'securityGroups' does not exist on type 'ApplicationLoadBalancer'.
    index.ts(40,55): error TS7006: Parameter 'sg' implicitly has an 'any' type.
    index.ts(47,13): error TS2339: Property 'attachTarget' does not exist on type 'ApplicationLoadBalancer'.
    index.ts(52,27): error TS2304: Cannot find name 'listener'.
I replaced index.ts with the code below
Copy code
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";
import * as pulumi from "@pulumi/pulumi";

export = async () => {
  const config = new pulumi.Config("aws");
  const providerOpts = { provider: new aws.Provider("prov", { region: <aws.Region>config.require("region") }) };
  // Create a security group to open ingress to our load balancer on port 80, and egress out of the VPC.
  const vpc = awsx.ec2.Vpc.getDefault();
  const sg = new awsx.ec2.SecurityGroup("web-sg", {
      vpc,
      // 1) Open ingress traffic to your load balancer. Explicitly needed for NLB, but not ALB:
      // ingress: [{ protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: [ "0.0.0.0/0" ] }],
      // 2) Open egress traffic from your EC2 instance to your load balancer (for health checks).
      egress: [{ protocol: "-1", fromPort: 0, toPort: 0, cidrBlocks: [ "0.0.0.0/0" ] }],
  });

  // Creates an ALB associated with the default VPC for this region and listen on port 80.
  // 3) Be sure to pass in our explicit SecurityGroup created above so that traffic may flow.
  const alb = new awsx.lb.ApplicationLoadBalancer("web-traffic", { securityGroups: [ sg ] });
  const listener = alb.createListener("web-listener", { port: 80 });
  const publicIps: pulumi.Output<string>[] = [];
  const subnets = await vpc.publicSubnets;

  // For each subnet, and each subnet/zone, create a VM and a listener.
  for (let i = 0; i < subnets.length; i++) {
      // 4) Create the instance in the same VPC, passing in the security group with egress rule.
      const vm = new aws.ec2.Instance(`web-${i}`, {
          ami: aws.getAmi({
              filters: [
                  { name: "name", values: [ "ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*" ] },
                  { name: "virtualization-type", values: [ "hvm" ] },
              ],
              mostRecent: true,
              owners: [ "099720109477" ], // Canonical
          }).then(ami => ami.id),
          instanceType: "t2.micro",
          subnetId: subnets[i].subnet.id,
          availabilityZone: subnets[i].subnet.availabilityZone,
          vpcSecurityGroupIds: alb.securityGroups.map(sg => sg.securityGroup.id),
          userData: `#!/bin/bash
  echo "Hello World, from Server ${i+1}!" > index.html
  nohup python -m SimpleHTTPServer 80 &`,
        }, providerOpts);
        publicIps.push(vm.publicIp);
        // 5) Attach your load balancer's target group the target EC2 instance(s).
        alb.attachTarget("target-" + i, vm);
      };
  }

  // Export the resulting URL so that it's easy to access.
  export const endpoint = listener.endpoint.hostname;
};
oh look slike it's missing curly brace
so something is wrong with the code from the other page
l

little-cartoon-10569

03/14/2023, 4:05 AM
I've never seen that
export = async () {
syntax before. I removed that and sorted the trailing
}
and it's good.
That might be for importing modules and having the code run on import? Sounds like a bad idea for IaC. Use classes. ComponentResources, for example.
a

acceptable-plumber-31485

03/14/2023, 4:06 AM
got it
4 Views