I want to launch a EC2 instance with an existing VPC. In the Vpc parameter I have passed existing VPC Id. When I execute cdk synth for the below code, I am getting All arguments to Vpc.fromLookup() must be concrete (no Tokens) error
using Amazon.CDK;
using Amazon.CDK.AWS.EC2;
using System.Collections.Generic;
using Amazon.CDK.AWS.S3;
using Amazon.CDK.AWS.S3.Assets;
using System.IO;
namespace StandardCf
{
public class StandardCfStack : Stack
{
internal StandardCfStack(Construct scope, string id, IStackProps props) : base(scope, id, props)
{
string[] instancetypeArray = new string[] { "t2.large","t3.large" };
//Parameters
//1. Key Pair Name
var keyPairName = new CfnParameter(this, "Key Pair Name", new CfnParameterProps { Type = "String", Description = "The name of the Existing Key Pair. This key pair will be added to the set of keys authorized for this instance." });
//2. Instance Type
var InstanceType = new CfnParameter( this, "InstanceLauncherType", new CfnParameterProps { Type = "String", Description= "Amazon EC2 instance type for the Instance, Choose t3.large for regions US East, Africa (Cape Town), Middle East (Bahrain), Asia Pacific (Hong Kong), EU (Milan) and EU (Stockholm)", AllowedValues = instancetypeArray, Default = "t2.large"} );
//3. Existing VPC Id
var ExistingVPCId = new CfnParameter(this, "VpcID", new CfnParameterProps { Type = "AWS::EC2::VPC::Id", Description = "Please enter the VPC ID to choose existing VPC"});
// VPC Creation
var vpc = Vpc.FromLookup(this, "VPC", new VpcLookupOptions
{
VpcId = ExistingVPCId.ValueAsString
});
// Security Group Creation
var InstanceSecurityGroup = new SecurityGroup(this, "SecurityGroup", new SecurityGroupProps
{
Vpc = vpc,
SecurityGroupName = "STANDARD-SG",
Description = "Security Group for Standard Instance",
AllowAllOutbound = true
});
// Security Group's Inbound and Outbound rules
InstanceSecurityGroup.AddIngressRule(Peer.AnyIpv4(), Port.Tcp(22), "Allows public SSH access");
InstanceSecurityGroup.AddIngressRule(Peer.AnyIpv4(), Port.Tcp(80), "Apache Web Server Access");
InstanceSecurityGroup.AddIngressRule(Peer.AnyIpv6(), Port.Tcp(80), "Apache Web Server Access");
InstanceSecurityGroup.AddIngressRule(Peer.AnyIpv4(), Port.Tcp(8080), "Apache Tomcat Access");
InstanceSecurityGroup.AddIngressRule(Peer.AnyIpv6(), Port.Tcp(8080), "Apache Tomcat Access");
InstanceSecurityGroup.AddIngressRule(Peer.AnyIpv4(), Port.Tcp(8161), "Apache ActiveMQ Web UI Access");
InstanceSecurityGroup.AddIngressRule(Peer.AnyIpv6(), Port.Tcp(8161), "Apache ActiveMQ Web UI Access");
InstanceSecurityGroup.AddIngressRule(Peer.AnyIpv4(), Port.Tcp(61616), "Apache ActiveMQ Broker Access");
InstanceSecurityGroup.AddIngressRule(Peer.AnyIpv6(), Port.Tcp(61616), "Apache ActiveMQ Broker Access");
// Configuring custom CENOTS AMI
IDictionary<string, string> d = new Dictionary<string, string>();
d.Add(new KeyValuePair<string, string>(Region,"ami-026f33d38b6410e30"));
var customAWSAMI = MachineImage.GenericLinux(d);
var path = Directory.GetCurrentDirectory();
// Comments for user data script
var userdata = UserData.ForLinux();
userdata.AddCommands("yum install -y wget", "cd /tmp/", "mkdir user-data-script", "cd user-data-script/", "wget some-url-for-shell-file", "cd ../../", "sh /tmp/user-data-script/shell-script.sh");
// Instance Detail Configuration
var ec2Instance = new Instance_(this, "Instance", new InstanceProps
{
Vpc = vpc,
InstanceType = new InstanceType(InstanceType.ValueAsString),
MachineImage = customAWSAMI,
SecurityGroup = InstanceSecurityGroup,
KeyName = keyPairName.ValueAsString,
InstanceName = "STANDARD",
UserData = userdata
});
}
}
}
How do I pass in existing VPC Id for the EC2 Instance?