{CloudWatch}複合アラームの作成

https://docs.aws.amazon.com/ja_jp/AmazonCloudWatch/latest/monitoring/Create_Composite_Alarm.html


-- 1. コマンド等のインストール

-- 1.1 aws cli version 2 インストール

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version

-- 1.2 jqインストール
sudo yum -y install jq

 

-- 2. SNSトピック作成

aws sns list-topics
aws sns list-subscriptions

aws sns create-topic --name topic01

aws sns subscribe \
--topic-arn arn:aws:sns:ap-northeast-1:999999999999:topic01 \
--protocol email \
--notification-endpoint hoge@example.com

 

-- 3. 複合アラームを作成


aws cloudwatch put-metric-alarm \
--alarm-name alarm01 \
--alarm-description "alarm01" \
--metric-name CPUUtilization \
--namespace AWS/EC2 \
--statistic Average \
--period 300 \
--threshold 30.0 \
--comparison-operator GreaterThanOrEqualToThreshold \
--dimensions '[
                {
                    "Name": "InstanceId",
                    "Value": "i-11111111111111111"
                }
            ]' \
--evaluation-periods 1 \
--datapoints-to-alarm 1 \
--treat-missing-data missing


aws cloudwatch put-metric-alarm \
--alarm-name alarm02 \
--alarm-description "alarm02" \
--metric-name EBSWriteBytes \
--namespace AWS/EC2 \
--statistic Average \
--period 300 \
--threshold 100000.0 \
--comparison-operator GreaterThanOrEqualToThreshold \
--dimensions '[
                {
                    "Name": "InstanceId",
                    "Value": "i-11111111111111111"
                }
            ]' \
--evaluation-periods 1 \
--datapoints-to-alarm 1 \
--treat-missing-data missing

 

aws cloudwatch put-composite-alarm \
--actions-enabled \
--alarm-actions arn:aws:sns:ap-northeast-1:999999999999:topic01 \
--alarm-description "alarm03" \
--alarm-name alarm03 \
--alarm-rule "ALARM("alarm01") AND ALARM("alarm02")"

 

aws cloudwatch describe-alarms


※管理者権限があるが、複合アラームがdescribe-alarmsで表示されない
「cloudwatch:DescribeAlarms」権限を付与しても変わらず

 

 

-- 4. 複合アラームをテスト

aws cloudwatch set-alarm-state \
--alarm-name alarm03 \
--state-reason "OK" \
--state-value OK

 

aws cloudwatch set-alarm-state \
--alarm-name alarm03 \
--state-reason "ALARM" \
--state-value ALARM

aws cloudwatch set-alarm-state \
--alarm-name alarm03 \
--state-reason "INSUFFICIENT_DATA" \
--state-value INSUFFICIENT_DATA


-- 5. クリーンアップ


-- アラームの削除

aws cloudwatch describe-alarms


aws cloudwatch delete-alarms \
--alarm-names alarm03

aws cloudwatch delete-alarms \
--alarm-names alarm02

aws cloudwatch delete-alarms \
--alarm-names alarm01


-- SNSトピック削除

aws sns unsubscribe --subscription-arn arn:aws:sns:ap-northeast-1:999999999999:topic01:11111111-2222-3333-4444-555555555555
aws sns delete-topic --topic-arn arn:aws:sns:ap-northeast-1:999999999999:topic01


aws sns list-topics
aws sns list-subscriptions

 

{ALB}固定レスポンス機能

https://dev.classmethod.jp/articles/alb-fixed-response/


-- 1. コマンド等のインストール

-- 1.1 aws cli version 2 インストール

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

aws --version

-- 1.2 jqインストール
sudo yum -y install jq

 

-- 2. ロードバランサーの作成


aws elbv2 create-load-balancer \
--name alb01  \
--subnets subnet-11111111111111111  subnet-22222222222222222 \
--security-groups sg-33333333333333333


aws elbv2 describe-load-balancers
aws elbv2 describe-load-balancers| jq -r .LoadBalancers.LoadBalancerArn

 


-- 3. リスナーの作成

aws elbv2 create-listener \
--load-balancer-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:loadbalancer/app/alb01/4444444444444444 \
--protocol HTTP \
--port 80  \
--default-actions '[
  {
      "Type": "fixed-response",
      "FixedResponseConfig": {
          "StatusCode": "200",
          "ContentType": "text/plain",
          "MessageBody": "Hello world"
      }
  }
]'

 


aws elbv2 describe-listeners \
--load-balancer-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:loadbalancer/app/alb01/4444444444444444

aws elbv2 describe-listeners \
--load-balancer-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:loadbalancer/app/alb01/4444444444444444 | jq -r .Listeners.ListenerArn

 

 

-- 4. 動作確認


http://alb01-6666666666.ap-northeast-1.elb.amazonaws.com

 

 

-- 5. クリーンアップ

 

-- リスナーの削除

aws elbv2 describe-listeners \
--load-balancer-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:loadbalancer/app/alb01/4444444444444444


aws elbv2 delete-listener \
--listener-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:listener/app/alb01/4444444444444444/5555555555555555

 

-- ロードバランサーの削除

aws elbv2 describe-load-balancers

aws elbv2 delete-load-balancer \
--load-balancer-arn arn:aws:elasticloadbalancing:ap-northeast-1:999999999999:loadbalancer/app/alb01/4444444444444444

 

 

 

 

{VPC}クロスリージョントランジットゲートウェイ

https://docs.aws.amazon.com/ja_jp/vpc/latest/tgw/transit-gateway-peering-scenario.html
https://docs.aws.amazon.com/ja_jp/vpc/latest/tgw/tgw-peering.html


前提: 
東京リージョンと大阪リージョンで以下を設定済み

サブネット作成
東京リージョン -> 172.31.48.0/24
大阪リージョン -> 10.0.1.0/24

作成したサブネットにEC2インスタンス作成
(セキュリティグループはping通信許可)

 

 

-- 1. コマンド等のインストール

-- 1.1 aws cli version 2 インストール

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version


-- 1.2 jqインストール
sudo yum -y install jq

 

-- 2. Transit Gateway作成 [ 東京リージョンでの作業 ]

export AWS_DEFAULT_REGION=ap-northeast-1

aws ec2 describe-transit-gateways

 

aws ec2 create-transit-gateway \
--options '{
  "AmazonSideAsn": 64512,
  "AutoAcceptSharedAttachments": "enable",
  "DefaultRouteTableAssociation": "enable",
  "DefaultRouteTablePropagation": "enable",
  "VpnEcmpSupport": "enable",
  "DnsSupport": "enable",
  "MulticastSupport": "disable"
}' \
--tag-specifications '[
  {
    "ResourceType": "transit-gateway",
    "Tags": [ {"Key": "Name", "Value" : "tgw01" } ]
  }
]'

 


-- 3. Transit Gatewayアタッチメントの作成 [ 東京リージョンでの作業 ]

export AWS_DEFAULT_REGION=ap-northeast-1

aws ec2 describe-transit-gateway-attachments
aws ec2 describe-transit-gateway-vpc-attachments
aws ec2 describe-transit-gateway-peering-attachments
aws ec2 describe-transit-gateway-route-tables

aws ec2 create-transit-gateway-vpc-attachment \
--transit-gateway-id tgw-00000000000000000 \
--vpc-id vpc-11111111111111111 \
--subnet-ids subnet-22222222222222222 \
--options '{
  "DnsSupport": "enable",
  "Ipv6Support": "disable",
  "ApplianceModeSupport": "disable"
}' \
--tag-specifications '[
  {
    "ResourceType": "transit-gateway-attachment",
    "Tags": [ {"Key": "Name", "Value" : "tgwa01" } ]
  }
]'

 

-- 4. Transit Gateway作成 [ 大阪リージョンでの作業 ]

export AWS_DEFAULT_REGION=ap-northeast-3

aws ec2 describe-transit-gateways

 

aws ec2 create-transit-gateway \
--options '{
  "AmazonSideAsn": 64512,
  "AutoAcceptSharedAttachments": "enable",
  "DefaultRouteTableAssociation": "enable",
  "DefaultRouteTablePropagation": "enable",
  "VpnEcmpSupport": "enable",
  "DnsSupport": "enable",
  "MulticastSupport": "disable"
}' \
--tag-specifications '[
  {
    "ResourceType": "transit-gateway",
    "Tags": [ {"Key": "Name", "Value" : "tgw02" } ]
  }
]'

 

 

-- 5. Transit Gatewayアタッチメントの作成 [ 大阪リージョンでの作業 ]

export AWS_DEFAULT_REGION=ap-northeast-3

aws ec2 describe-transit-gateway-attachments
aws ec2 describe-transit-gateway-vpc-attachments
aws ec2 describe-transit-gateway-peering-attachments
aws ec2 describe-transit-gateway-route-tables


aws ec2 create-transit-gateway-vpc-attachment \
--transit-gateway-id tgw-33333333333333333 \
--vpc-id vpc-44444444444444444 \
--subnet-ids subnet-55555555555555555 \
--options '{
  "DnsSupport": "enable",
  "Ipv6Support": "disable",
  "ApplianceModeSupport": "disable"
}' \
--tag-specifications '[
  {
    "ResourceType": "transit-gateway-attachment",
    "Tags": [ {"Key": "Name", "Value" : "tgwa02" } ]
  }
]'

 


-- 6. Transit Gateway ピアリングアタッチメントの作成 [ 東京リージョンでの作業、大阪リージョンでの作業 ]

ピアリングアタッチメントをアクティブにするには、アクセプタ Transit Gateway の所有者がピアリングアタッチメントリクエストを受け入れる必要があります。
これは、両方の Transit Gateway が同じアカウントにある場合でも必要です。


export AWS_DEFAULT_REGION=ap-northeast-1

aws ec2 describe-transit-gateway-attachments
aws ec2 describe-transit-gateway-vpc-attachments
aws ec2 describe-transit-gateway-peering-attachments
aws ec2 describe-transit-gateway-route-tables


aws ec2 create-transit-gateway-peering-attachment \
--transit-gateway-id tgw-00000000000000000 \
--peer-transit-gateway-id tgw-33333333333333333 \
--peer-account-id 999999999999 \
--peer-region ap-northeast-3 \
--tag-specifications '[
  {
    "ResourceType": "transit-gateway-attachment",
    "Tags": [ {"Key": "Name", "Value" : "tgwpa01" } ]
  }
]'

export AWS_DEFAULT_REGION=ap-northeast-3

aws ec2 accept-transit-gateway-peering-attachment \
--transit-gateway-attachment-id tgw-attach-66666666666666666


-- 7. Transit Gatewayのルートテーブルに経路追加 [ 東京リージョン、大阪リージョンでの作業 ]

ピアリングされた Transit Gateway 間でトラフィックをルーティングするには、
Transit Gateway のピアリングアタッチメントをポイントする静的ルートを Transit Gateway のルートテーブルに追加する必要があります。


export AWS_DEFAULT_REGION=ap-northeast-1

aws ec2 create-transit-gateway-route \
--destination-cidr-block 10.0.1.0/24 \
--transit-gateway-route-table-id tgw-rtb-77777777777777777 \
--transit-gateway-attachment-id tgw-attach-66666666666666666

 


export AWS_DEFAULT_REGION=ap-northeast-3

aws ec2 create-transit-gateway-route \
--destination-cidr-block 172.31.48.0/24  \
--transit-gateway-route-table-id tgw-rtb-88888888888888888 \
--transit-gateway-attachment-id tgw-attach-66666666666666666

 


-- 8. サブネットのルートテーブルに経路追加 [ 東京リージョン、大阪リージョンでの作業 ]

大阪リージョン:
 172.31.48.0/24 -> TGW

 

東京リージョン:
 10.0.1.0/24 -> TGW

 

-- 9. 動作確認  [ 東京リージョンでの作業 ]

ping 10.0.1.150

 

-- 10. クリーンアップ


-- Transit Gateway ピアリングアタッチメントの削除  [ 東京リージョンでの作業 ]

export AWS_DEFAULT_REGION=ap-northeast-1

aws ec2 describe-transit-gateway-attachments
aws ec2 describe-transit-gateway-vpc-attachments
aws ec2 describe-transit-gateway-peering-attachments
aws ec2 describe-transit-gateway-route-tables


aws ec2 delete-transit-gateway-peering-attachment \
--transit-gateway-attachment-id tgw-attach-66666666666666666

 

-- Transit Gatewayアタッチメントの削除 [ 大阪リージョンでの作業 ]

export AWS_DEFAULT_REGION=ap-northeast-3

aws ec2 describe-transit-gateway-attachments
aws ec2 describe-transit-gateway-vpc-attachments
aws ec2 describe-transit-gateway-peering-attachments
aws ec2 describe-transit-gateway-route-tables

aws ec2 delete-transit-gateway-vpc-attachment \
--transit-gateway-attachment-id tgw-attach-aaaaaaaaaaaaaaaaa

 

-- Transit Gatewayの削除 [ 大阪リージョンでの作業 ]

export AWS_DEFAULT_REGION=ap-northeast-3

aws ec2 describe-transit-gateways

aws ec2 delete-transit-gateway \
--transit-gateway-id tgw-33333333333333333


-- Transit Gatewayアタッチメントの削除 [ 東京リージョンでの作業 ]

export AWS_DEFAULT_REGION=ap-northeast-1

aws ec2 describe-transit-gateway-attachments
aws ec2 describe-transit-gateway-vpc-attachments
aws ec2 describe-transit-gateway-peering-attachments
aws ec2 describe-transit-gateway-route-tables

aws ec2 delete-transit-gateway-vpc-attachment \
--transit-gateway-attachment-id tgw-attach-bbbbbbbbbbbbbbbbb

 

-- Transit Gatewayの削除 [ 東京リージョンでの作業 ]

export AWS_DEFAULT_REGION=ap-northeast-1

aws ec2 describe-transit-gateways

aws ec2 delete-transit-gateway \
--transit-gateway-id tgw-00000000000000000

 

 

{VPC}クロスアカウントトランジットゲートウェイ


https://dev.classmethod.jp/articles/transit-gateway-vpc-account/
https://dev.classmethod.jp/articles/transitgateway-cross-account-diagram/

共有元アカウント(アカウントA): 999999999999
共有先アカウント(アカウントB): 888888888888

前提: 
共有元と共有先で以下を設定済み

サブネット作成
アカウントA -> 172.31.48.0/24
アカウントB -> 10.0.1.0/24

作成したサブネットにEC2インスタンス作成
(セキュリティグループはping通信許可)

 

 

-- 1. コマンド等のインストール

-- 1.1 aws cli version 2 インストール

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version


-- 1.2 jqインストール
sudo yum -y install jq

 

-- 2. Transit Gateway作成 [ アカウントAでの作業 ]


aws ec2 describe-transit-gateways

 

aws ec2 create-transit-gateway \
--options '{
  "AmazonSideAsn": 64512,
  "AutoAcceptSharedAttachments": "enable",
  "DefaultRouteTableAssociation": "enable",
  "DefaultRouteTablePropagation": "enable",
  "VpnEcmpSupport": "enable",
  "DnsSupport": "enable",
  "MulticastSupport": "disable"
}' \
--tag-specifications '[
  {
    "ResourceType": "transit-gateway",
    "Tags": [ {"Key": "Name", "Value" : "tgw01" } ]
  }
]'

 


-- 3. Transit Gatewayアタッチメントの作成 [ アカウントAでの作業 ]


aws ec2 describe-transit-gateway-attachments
aws ec2 describe-transit-gateway-vpc-attachments
aws ec2 describe-transit-gateway-route-tables

aws ec2 create-transit-gateway-vpc-attachment \
--transit-gateway-id tgw-00000000000000000 \
--vpc-id vpc-11111111111111111 \
--subnet-ids subnet-22222222222222222 \
--options '{
  "DnsSupport": "enable",
  "Ipv6Support": "disable",
  "ApplianceModeSupport": "disable"
}' \
--tag-specifications '[
  {
    "ResourceType": "transit-gateway-attachment",
    "Tags": [ {"Key": "Name", "Value" : "tgwa01" } ]
  }
]'

 

 

-- 4. Transit Gateway共有 [ アカウントAでの作業 ]


aws ram list-resources --resource-owner SELF


aws ram create-resource-share \
--name rs01 \
--resource-arns arn:aws:ec2:ap-northeast-1:999999999999:transit-gateway/tgw-00000000000000000 \
--principals 888888888888 \
--no-allow-external-principals

 

-- 5. Transit Gatewayアタッチメントの作成 [ アカウントBでの作業 ]

aws sts get-caller-identity

aws ec2 describe-transit-gateway-attachments
aws ec2 describe-transit-gateway-vpc-attachments
aws ec2 describe-transit-gateway-route-tables


aws ec2 create-transit-gateway-vpc-attachment \
--transit-gateway-id tgw-00000000000000000 \
--vpc-id vpc-33333333333333333 \
--subnet-ids subnet-44444444444444444 \
--options '{
  "DnsSupport": "enable",
  "Ipv6Support": "disable",
  "ApplianceModeSupport": "disable"
}' \
--tag-specifications '[
  {
    "ResourceType": "transit-gateway-attachment",
    "Tags": [ {"Key": "Name", "Value" : "tgwa01" } ]
  }
]'

 


-- 6. サブネットのルートテーブルに経路追加 [ アカウントA、アカウントBでの作業 ]

アカウントB:
 172.31.48.0/24 -> TGW

 

アカウントA:
 10.0.1.0/24 -> TGW

 

-- 7. 動作確認  [ アカウントAでの作業 ]

ping 10.0.1.226

 

-- 8. クリーンアップ


-- Transit Gatewayアタッチメントの削除 [ アカウントBでの作業 ]


aws ec2 describe-transit-gateway-attachments
aws ec2 describe-transit-gateway-vpc-attachments
aws ec2 describe-transit-gateway-route-tables

aws ec2 delete-transit-gateway-vpc-attachment \
--transit-gateway-attachment-id tgw-attach-66666666666666666


-- Transit Gateway共有解除  [ アカウントAでの作業 ]

aws ram list-resources --resource-owner SELF

aws ram delete-resource-share \
--resource-share-arn arn:aws:ram:ap-northeast-1:999999999999:resource-share11111111-2222-3333-4444-555555555555

 

-- Transit Gatewayアタッチメントの削除 [ アカウントAでの作業 ]

aws ec2 describe-transit-gateway-attachments
aws ec2 describe-transit-gateway-vpc-attachments
aws ec2 describe-transit-gateway-route-tables

aws ec2 delete-transit-gateway-vpc-attachment \
--transit-gateway-attachment-id tgw-attach-77777777777777777

 

-- Transit Gatewayの削除 [ アカウントAでの作業 ]

aws ec2 describe-transit-gateways

aws ec2 delete-transit-gateway \
--transit-gateway-id tgw-00000000000000000

 

 

 

 

リストパーティションのメンテナンス

(8.0.28)


drop table tab1;

create table tab1
( col1   int not null
, col2   varchar(10) not null
)
 partition by list columns ( col2 )
 ( partition p1 values in ('AX','AY')
 , partition p2 values in ('BX','BY')
 , partition p3 values in ('CX','CY')
 , partition p4 values in ('DX','DY')
 );

alter table tab1 add constraint tab1pk primary key(col1,col2);


insert into tab1 values(10,'AX');
insert into tab1 values(20,'BX');
insert into tab1 values(30,'CX');
insert into tab1 values(40,'DX');

select * from tab1;
select * from tab1 partition(p1);
select * from tab1 partition(p2);
select * from tab1 partition(p3);
select * from tab1 partition(p4);


select table_name, partition_name, partition_expression, partition_description
from information_schema.partitions
where table_schema = 'test'
and table_name = 'tab1'
;


-- ①交換

drop table tab2;
create table tab2
( col1   int not null
, col2   varchar(10) not null
);
alter table tab2 add constraint tab2pk primary key(col1,col2);


insert into tab2 values(21,'BY');
select * from tab2;

alter table tab1 exchange partition p2 with table tab2;


-- ②分割

alter table tab1
  reorganize partition p2 into (
   partition p21 values in ('BX')
  ,partition p22 values in ('BY')
);


select * from tab1 partition(p21);
select * from tab1 partition(p22);

-- ③マージ

alter table tab1
  reorganize partition p21,p22 into (
   partition p2 values in ('BX','BY')
);


-- ④追加
alter table tab1 add partition (partition p5 values in ('EX','EY') );

insert into tab1 values(50,'EX');
select * from tab1 partition(p5);

-- ⑤削除
alter table tab1 drop partition p5;

 

 

 

(19c)


drop table tab1 purge;

create table tab1
( col1   int not null primary key
, col2   varchar2(10) not null
)
 partition by list (col2)
 ( partition p1 values ('AX','AY')
 , partition p2 values ('BX','BY')
 , partition p3 values ('CX','CY')
 , partition p4 values ('DX','DY')
 );


insert into tab1 values(10,'AX');
insert into tab1 values(20,'BX');
insert into tab1 values(30,'CX');
insert into tab1 values(40,'DX');
commit;

select * from tab1;
select * from tab1 partition(p1);
select * from tab1 partition(p2);
select * from tab1 partition(p3);
select * from tab1 partition(p4);

select table_name, partition_name, high_value
from dba_tab_partitions
where table_owner = 'TEST'
and table_name = 'TAB1'
;

-- ①交換
drop table tab2 purge;
create table tab2
( col1   int not null primary key
, col2   varchar2(10) not null
);


insert into tab2 values(21,'BY');
commit;
select * from tab2;

alter table tab1 exchange partition p2 with table tab2 update indexes;

-- ②分割

alter table tab1
  split partition p2 values ('BX') 
  into ( partition p21, partition p22)
  update indexes;

select * from tab1 partition(p21);
select * from tab1 partition(p22);

-- ③マージ
alter table tab1 
  merge partitions p21, p22
  into partition p2
  update indexes;

-- ④追加
alter table tab1 add partition p5 values('EX','EY') update indexes;

insert into tab1 values(50,'EX');
select * from tab1 partition(p5);

-- ⑤削除
alter table tab1 drop partition p5 update indexes;

 

 

(14)

drop table tab1 cascade;

create table tab1
( col1   int not null
, col2   varchar(10) not null
)
partition by list ( col2 )
;

alter table tab1 add constraint tab1pk primary key(col1,col2);

create table tab1p1 partition of tab1 for values in ('AX','AY');
create table tab1p2 partition of tab1 for values in ('BX','BY');
create table tab1p3 partition of tab1 for values in ('CX','CY');
create table tab1p4 partition of tab1 for values in ('DX','DY');

insert into tab1 values(10,'AX');
insert into tab1 values(20,'BX');
insert into tab1 values(30,'CX');
insert into tab1 values(40,'DX');

select * from tab1;
select * from tab1p1;
select * from tab1p2;
select * from tab1p3;
select * from tab1p4;

select * from pg_partitioned_table;

select t1.inhparent, t2.relname, t1.inhrelid, t3.relname
from pg_inherits t1 
inner join pg_class t2
on t1.inhparent = t2.oid
inner join pg_class t3
on t1.inhrelid = t3.oid
where t1.inhparent = 'tab1'::regclass::oid
;


-- ①交換
構文はない模様
-- ②分割
構文はない模様
-- ③マージ
構文はない模様

-- ④追加
drop table tab1p5;

create table tab1p5
( col1   int not null
, col2   varchar(10) not null
)
;

alter table tab1 attach partition tab1p5 for values in ('EX','EY');

insert into tab1 values(50,'EX');
select * from tab1p5;
select * from tab1;

-- ⑤削除
alter table tab1 detach partition tab1p5;

select * from tab1p5;
select * from tab1;

 

(2019)

リストパーティション未対応