Skip to content

Commit

Permalink
TFでポータルサイトを自動構築する部分はできた。あとはベンチマーカーがAPI Gatewayに結果を送信する部分
Browse files Browse the repository at this point in the history
  • Loading branch information
showwin committed Oct 19, 2024
1 parent 133677d commit 69fe639
Show file tree
Hide file tree
Showing 16 changed files with 387 additions and 24 deletions.
2 changes: 1 addition & 1 deletion contest/portal/dist/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const apiUrl = "https://<API_GATEWAY_DOMAIN_NAME>/teams";
const apiUrl = "<<API_GATEWAY_DOMAIN_URL>>teams";
// Function to fetch the latest data
function fetchData() {
return __awaiter(this, void 0, void 0, function* () {
Expand Down
2 changes: 1 addition & 1 deletion contest/portal/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface TeamScore {
timestamp: string;
}

const apiUrl = "https://<API_GATEWAY_DOMAIN_NAME>/teams";
const apiUrl = "<<API_GATEWAY_DOMAIN_URL>>teams";

// Function to fetch the latest data
async function fetchData() {
Expand Down
5 changes: 4 additions & 1 deletion contest/terraform/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.terraform
terraform.tfstate*
*terraform.tfstate*
*.py
*.zip
*.js
26 changes: 21 additions & 5 deletions contest/terraform/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# terraform
# Terraform

## 概要

terraformでISHOCON1の環境が構築できます
TerraformでISHOCON1の環境が構築できます
社内ISUCON等のコンテストの開催準備として使うと便利です。
開催者(以下admins)と参加者(以下teams)のロールに分けて、サーバーを複数台準備できます。

## 作成されるもの
* チーム毎に1台の競技に使用するEC2インスタンス
* 各チームのスコアがリアルタイムに見れるポータルサイト

## 必要なもの

- 支払情報が紐付けされたAWSアカウント
- ポータルサイトは低コストになるように実装していますが、主に以下の費用がかかります。
- DynamoDB, Lambda, CloudWatch, API Gateway, S3
- IAMユーザのアクセスIDとシークレットアクセスキー

## 手順
Expand Down Expand Up @@ -40,9 +45,20 @@ $ ssh -T [email protected]
```shell
$ cd main
$ terraform apply

...

Outputs:

apigateway_url = "https://123456789.execute-api.ap-northeast-1.amazonaws.com/"
ip_addr = {
"team1" = "11.111.111.111"
"team2" = "11.111.111.112"
}
portal_url = "http://ishocon1-portal123456789.s3-website-ap-northeast-1.amazonaws.com"
```

outputに競技で使用するインスタンスのIPアドレスが出力されるので、参加者に共有する。
Outputに競技で使用するインスタンスのIPアドレスとポータルサイトのアドレスが出力されるので、参加者に共有する。

```
$ ssh ishocon@<instance_ip>
Expand All @@ -53,5 +69,5 @@ $ ssh ishocon@<instance_ip>
## 注意点

- デフォルトでは、ISHOCON1の推奨インスタンスタイプである `c7i.xlarge` でインスタンスが起動します
- このterraformを使用することで発生した問題に対して責任は一切取りません
- このTerraformを使用することで発生した問題に対して責任は一切取りません
- コードをよく読んだ上で、自己責任でご使用ください
39 changes: 39 additions & 0 deletions contest/terraform/main/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contest/terraform/main/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module "main" {
source = "../module"

admins = local.admins
teams = local.teams
teams = local.teams

use_spot_instance = false
}
8 changes: 8 additions & 0 deletions contest/terraform/main/output.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
output "ip_addr" {
value = module.main.ip_addr
}

output "portal_url" {
value = module.main.portal_url
}

output "apigateway_url" {
value = module.main.apigateway_url
}
6 changes: 3 additions & 3 deletions contest/terraform/main/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ terraform {
}

provider "aws" {
region = "ap-northeast-1"
region = "ap-northeast-1"

default_tags {
tags = {
Service = "ISHOCON1"
ManagedBy = "Terraform"
Service = "ISHOCON1"
ManagedBy = "Terraform"
}
}
}
2 changes: 2 additions & 0 deletions contest/terraform/module/data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data "aws_caller_identity" "current" {}
data "aws_region" "current" {}
8 changes: 4 additions & 4 deletions contest/terraform/module/ec2.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ EOF
}

resource "aws_ec2_tag" "instance_name" {
for_each = aws_instance.main
for_each = aws_instance.main

resource_id = each.value.id
key = "Name"
value = "ISHOCON1 - ${each.key}"
value = "${var.name} - ${each.key}"
}

resource "aws_ec2_tag" "instance_team_name" {
for_each = aws_instance.main
for_each = aws_instance.main

resource_id = each.value.id
key = "team_name"
value = each.key
}

resource "aws_ec2_tag" "instance_players" {
for_each = aws_instance.main
for_each = aws_instance.main

resource_id = each.value.id
key = "players"
Expand Down
8 changes: 4 additions & 4 deletions contest/terraform/module/ec2_spot.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ EOF
}

resource "aws_ec2_tag" "spot_instance_name" {
for_each = aws_spot_instance_request.main
for_each = aws_spot_instance_request.main

resource_id = each.value.spot_instance_id
key = "Name"
value = "ISHOCON1 - ${each.key}"
value = "${var.name} - ${each.key}"
}

resource "aws_ec2_tag" "spot_instance_team_name" {
for_each = aws_spot_instance_request.main
for_each = aws_spot_instance_request.main

resource_id = each.value.spot_instance_id
key = "team_name"
value = each.key
}

resource "aws_ec2_tag" "spot_instance_players" {
for_each = aws_spot_instance_request.main
for_each = aws_spot_instance_request.main

resource_id = each.value.spot_instance_id
key = "players"
Expand Down
10 changes: 9 additions & 1 deletion contest/terraform/module/output.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
output "ip_addr" {
value = {
for team in local.team_list :
team => try(aws_instance.main[team]["public_ip"] , aws_spot_instance_request.main[team]["public_ip"])
team => try(aws_instance.main[team]["public_ip"], aws_spot_instance_request.main[team]["public_ip"])
}
}

output "portal_url" {
value = "http://${aws_s3_bucket_website_configuration.portal.website_endpoint}"
}

output "apigateway_url" {
value = aws_apigatewayv2_stage.portal.invoke_url
}
Loading

0 comments on commit 69fe639

Please sign in to comment.