Github action を利用して、EC2サーバにログインすると、Security Group 重複のため、下記のエラーが出てしまう。
aws ec2 authorize-security-group-ingress \
--group-id='sg-xxxxxxx' \
--protocol=tcp \
--port=22 \
--cidr=xxx.xxx.xxx.xxx/32
An error occurred (InvalidPermission.Duplicate) when calling the AuthorizeSecurityGroupIngress operation: the specified rule "peer: xxx.xxx.xxx.xxx/32, TCP, from port: ***, to port: ***, ALLOW" already exists
Error: Process completed with exit code 254.
Security Group 重複回避方法
set +e # Disable immediate exit on error
aws ec2 authorize-security-group-ingress \
--group-id='sg-xxxxxxx' \
--protocol=tcp \
--port=22 \
--cidr=xxx.xxx.xxx.xxx/32
# avoid the InvalidPermission.Duplicate error
if [ $? -eq 254 ]; then
echo "Rule already exists."
fi
set -e # Re-enable immediate exit on error