https://pulumi.com logo
#general
Title
# general
d

damp-lock-9822

05/23/2023, 8:03 PM
Hi all, I’m trying to create an aws Ec2 instance using the aws-classic pulumi package. I defined an explicit provider in order to include the assumeRole credentials needed to make the instance in the target account. I’m facing a couple errors that have stumped me and any guidance would be much appreciated. Despite that the provider is both specified in my getAmi call and is defined properly, I am getting the error
Unhandled exception: Error: Invoke: Default provider for 'aws' disabled. 'aws:ec2/getAmi:getAmi' must use an explicit provider
.
b

billowy-army-68599

05/23/2023, 8:11 PM
could you share your code?
d

damp-lock-9822

05/23/2023, 8:23 PM
const newFixture = (fixture: Fixture) => {
const fixturePulumiProgram = () => {
const fixtures = fixture.map((fix) => {
switch (fix.fixtureType) {
case "azure":
return new AzureFixtureComponent(fix.baseName, fix, configuration, {});
case "aws": {
const commonProviderArgs = {
accessKey: this.awsConfig.AWS_ACCESS_KEY,
secretKey: pulumi.secret(this.awsConfig.AWS_SECRET_KEY),
assumeRole: {
externalId: this.awsConfig.EXTERNAL_ID,
roleArn: this.awsConfig.ROLE_ARN,
},
};
const awsClassicProvider = new awsClassic.Provider("aws-classic-test", {
...commonProviderArgs,
region: 'us-east-2',
});
pulumi.all([fix, awsClassicProvider]).apply(([fixture, awsProvider]) => {
const awsFix = new AwsFixtureComponent(fixture.baseName, fixture, configuration, awsConfig, awsProvider, {});
awsFix
.initialize()
.then((fix) => {
return fix;
})
.catch((err) => {
log.error(err);
});
return awsFix;
});
}
}
});
return Promise.all(fixtures);
};
const baseStackName = demo-fixture;
const stackDestroy = fixture.map((fix) => fix.destroy);
const fixtureStack = async () =>
await pulumi.automation.LocalWorkspace.createOrSelectStack(
{
program: fixturePulumiProgram,
stackName: baseStackName,
projectName: ${baseStackName}-project,
},
{
workDir: "stackWorkDir",
}
).then((stack) => {
if (stackDestroy.map((fix) => fix).includes(true)) {
//bleh
stack
.destroy({
onOutput: process.stdout.write.bind(process.stdout),
})
.catch(
logErrorMessage({
stackname: stack.name,
})
);
}
stack
.up({
onOutput: process.stdout.write.bind(process.stdout),
})
.catch(
logErrorMessage({
stackname: stack.name,
})
);
});
fixtureStack().catch(
logErrorMessage({
stackname: "undefined",
})
);
};
export class AwsFixtureComponent extends pulumi.ComponentResource {
public readonly fixture: AwsFixture;
public readonly config: Configuration;
public readonly awsConfig: AwsConfig;
public readonly provider: aws.Provider;
constructor(name: string, fixture: AwsFixture, config: Configuration, awsConfig: AwsConfig, provider: aws.Provider, opts?: pulumi.ComponentResourceOptions) {
super("fixtures:awsFixture", name, {}, opts);
this.fixture = fixture;
this.config = config;
this.awsConfig = awsConfig;
this.provider = provider;
}
async initialize() {
const fixtureAmi = await aws.ec2
.getAmi({
filters: [
{
name: "name",
values: ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"],
},
],
mostRecent: true,
},
{ provider: this.provider })
.then((invoke) => invoke.id);
const fixtureVpc = await aws.ec2.getVpc(
{ id: this.fixture.awsVpcId },
{ provider: this.provider });
const fixtureSubnet = await aws.ec2.getSubnet({
id: this.fixture.awsSubId,
},
{ provider: this.provider });
const fixtureNetworkInterface = new aws.ec2.NetworkInterface(${this.fixture.baseName}-ni, {
subnetId: this.fixture.awsSubId,
tags: {
Name: "primary_network_interface",
Stage: "Demo",
},
},
{ provider: this.provider });
const fixtureEc2 = new aws.ec2.Instance(${this.fixture.baseName}-server, {
instanceType: "t3.micro",
subnetId: fixtureSubnet.id,
networkInterfaces: [
{
networkInterfaceId: new aws.ec2.NetworkInterface(${this.fixture.baseName}-ni, {
subnetId: this.fixture.awsSubId,
tags: {
Name: "primary_network_interface",
Stage: "Demo",
},
},
{ provider: this.provider }).id,
deviceIndex: 0,
},
],
ami: fixtureAmi,
tags: {
Name: "webserver",
Stage: "Demo",
},
},
{ provider: this.provider });
// Instance's publicly accessible IP address and hostname.
const ip = fixtureEc2.publicIp;
const hostname = fixtureEc2.publicDns;
`const url = pulumi.interpolate`http://${fixtureEc2.publicDns}`;`
this.registerOutputs({
fixtureVpc,
fixtureEc2,
ip,
hostname,
url,
});
return [fixtureVpc, fixtureEc2, ip, hostname, url];
}
}
5 Views