import { App, Stack, S3Backend } from "tfts";
import { AwsProvider } from "./.gen/providers/aws/provider";
import { Vpc } from "./.gen/providers/aws/vpc";
import { Subnet } from "./.gen/providers/aws/subnet";
import { SecurityGroup } from "./.gen/providers/aws/security-group";
import { Instance } from "./.gen/providers/aws/instance";
class MyAwsStack extends Stack {
constructor(scope: App, id: string) {
super(scope, id);
// Configure the AWS Provider
new AwsProvider(this, "aws", {
region: "us-west-2",
});
// Configure S3 Backend
new S3Backend(this, {
bucket: "my-terraform-state-bucket",
key: "network/terraform.tfstate",
region: "us-west-2",
});
// Create a VPC
const vpc = new Vpc(this, "main-vpc", {
cidrBlock: "10.0.0.0/16",
enableDnsHostnames: true,
tags: {
Name: "main-vpc",
},
});
// Create a Subnet
const subnet = new Subnet(this, "main-subnet", {
vpcId: vpc.id,
cidrBlock: "10.0.1.0/24",
availabilityZone: "us-west-2a",
tags: {
Name: "main-subnet",
},
});
// Create a Security Group
const sg = new SecurityGroup(this, "web-sg", {
vpcId: vpc.id,
ingress: [
{
fromPort: 80,
toPort: 80,
protocol: "tcp",
cidrBlocks: ["0.0.0.0/0"],
},
{
fromPort: 22,
toPort: 22,
protocol: "tcp",
cidrBlocks: ["0.0.0.0/0"],
},
],
egress: [
{
fromPort: 0,
toPort: 0,
protocol: "-1",
cidrBlocks: ["0.0.0.0/0"],
},
],
});
// Create an EC2 Instance
new Instance(this, "web-server", {
ami: "ami-0c55b159cbfafe1f0", // Amazon Linux 2
instanceType: "t3.micro",
subnetId: subnet.id,
vpcSecurityGroupIds: [sg.id],
tags: {
Name: "web-server",
},
});
}
}
const app = new App();
new MyAwsStack(app, "aws-example");
app.synth();