this is complete connection block. try using a complete block
connection { type = "ssh" user = "ec2-user" private_key = file("keypair2.pem") host = aws_instance.my_instance.public_ip }